Reputation: 673
I'm currently scaling down Bitmaps in order to avoid an OutOfMemoryException. Is there a way to specify a file size to decode to? So no matter what file size the input image is, it will be scaled down to the specified file size.
I'm using the following code from http://androidbysravan.wordpress.com/category/outofmemory-exception-when-decoding-with-bitmapfactory/ until I can figure out a way to specify the output file size.
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
AssetFileDescriptor fileDescriptor =null;
try {
fileDescriptor = _context.getContentResolver().openAssetFileDescriptor(selectedImage,"r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
try {
bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
fileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;
}
Upvotes: 0
Views: 1775
Reputation: 20563
The images you decode will be in a compressed format such as PNG, JPEG etc. So when you decode them, all you are doing is decompressing them into memory in a format that android can understand. Although you cannot control the size of the decoded images, You can however, Control certain properties that allow you to decode the images efficiently using the BitmapFactory.Options
class.
Here's the excellent official guide on how to properly use this class to load your images efficiently:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Upvotes: 1