So (or SO), my requirements are:
- Display 4 photo placeholders
- Click one placeholder to take a photo
- Display the 4 thumbnails of the taken pictures in a fragment
- 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:
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; }
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); }
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.