I am having an intermittent problem with capturing an image from the Camera activity, saving it to a smaller size, and uploading the saved image to my server.
If an image file is larger than a particular threshold (I use 2,000KB) I'm calling the following function to downsample it and save the smaller image:
private void downsampleLargePhoto(Uri uri, int fileSizeKB)
{
int scaleFactor = (int) (fileSizeKB / fileSizeLimit);
log("image is " + scaleFactor + " times too large");
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try
{
options.inJustDecodeBounds = false;
options.inSampleSize = scaleFactor;
Bitmap scaledBitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(uri), null, options);
log("scaled bitmap has size " + scaledBitmap.getWidth() + " x " + scaledBitmap.getHeight());
String scaledFilename = uri.getPath();
log("save scaled image to file " + scaledFilename);
FileOutputStream out = new FileOutputStream(scaledFilename);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
scaledBitmap.recycle();
File image = new java.io.File(scaledFilename);
int newFileSize = (int) image.length()/1000;
log("scaled image file is size " + newFileSize + " KB");
}
catch(FileNotFoundException f)
{
log("FileNotFoundException: " + f);
}
}
With very large images, however, my app crashes with an OutOfMemoryError on the line:
Bitmap scaledBitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(uri), null, options);
What else can I do at this point to shrink an image?