1

So (or SO), my requirements are:

  1. Display 4 photo placeholders
  2. Click one placeholder to take a photo
  3. Display the 4 thumbnails of the taken pictures in a fragment
  4. Upload 4 photos (one unique HTTP request, max size per photo 250kb, total 1 MB)

I initially thought this was a standard/quite easy task, but I finally changed my mind. I'm proceeding this way:

  1. When a user click on a placeholder:

    file = createImageFile();
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    startActivityForResult(takePictureIntent);
    
    private File createImageFile() throws IOException {
    
        // fileName is just a unique string...
    
        File storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    
        File file = File.createTempFile(fileName, ".jpg", storageDir);
    
        return file;
    }
    
  2. From Google Docs I set the thumbnail for the placeholder:

    private void setThumbnail(File file, ImageView target) {
        int targetWidth = target.getWidth();
        int targetHeight = target.getHeight();
    
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        int thumbWidth = options.outWidth;
        int thumbHeight = options.outHeight;
    
        int scaleFactor = Math.min(thumbWidth / targetWidth, thumbHeight
                / targetHeight);
    
        options.inJustDecodeBounds = false;
        options.inSampleSize = scaleFactor;
        options.inPurgeable = true;
    
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(),
            options);
    
        target.setImageBitmap(bitmap);
    }
    
  3. Server side my PHP API requires 4 JPEG files 250kb max each. I'm using Loopj library (1.4.6) to upload them. Loopj set request parameters whit method put, and we can use three way to upload a file:

    params.put("picture", file); // Upload a File
    params.put("picture", inputStream); // Upload an InputStream
    params.put("picture", new ByteArrayInputStream(bytes)); // Upload some bytes
    

The pictures saved from camera intent are too big for the memory reserved to the app and I cannot just "load" them in RAM (or I'll get a OOM Exception).

Should I scale them down (or compress don't know) iteratively to 250kb (or some specific pixels size) and then overwrite the saved files? Can you please suggest something to point me in the right direction?

Important: if possile I'd like to avoid rewriting a camera app, I'm just going with the intent, I've not enough time.

Jumpa
  • 4,319
  • 11
  • 52
  • 100

1 Answers1

0

Did you try to add in your manifest.xml that code:

<application 
       android:largeHeap="true">

That allows you to manage properly huge photo by allocating more memory to your app.

Phil
  • 4,730
  • 1
  • 41
  • 39
  • I'd prefer avoiding this option due to limited compatibility and support. Instead i want to reduce memory usage, handling smaller photos. Anyway thanks for your suggestion. – Jumpa Feb 09 '15 at 14:17