I have few questions about handling large bitmaps, I couldn’t find answer on the topics I found where this issue was discussed so far.
I have a Nexus S which when I take an image with the hardware.Camera class like this:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
if (imageData != null) {
BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
}
}
};
Having in mind that the image is 5MPixels, the application at the point of decodeByteArray crashes.
So I thought, if this crashes then how is it done in the Camera app of Android.
I downloaded the source and there I found the makeBitmap method there:
http://www.java2s.com/Open-Source/Android/android-platform-apps/Gallery/com/android/camera/Util.java.htm
So I changed my callback to use makeBitmap:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
if (imageData != null) {
Bitmap bitmap = Utils.makeBitmap(imageData, 50 * 1024);
}
}
};
So I took that method and used in my app. Not only that the image I create with this method is in low quality, but also if I make the number of pixels (50 * 1024) bigger, I will have another OutOfMemory issue.
So my question would be, if i want to use big bitmaps, and by big I mean a PNG with alpha layers at about 500×300 in size, how can I do it? How is the creation of the big Image in the Android Camera app being done actually?
Thanks!
You won’t be able to fit that large of a bitmap into memory for display.
But you can save out the bitmap:
And later load in a downsampled version for display.
That way you keep the original sized image, while still being able to display the image without an overflow.
Note: You’ll need to use options to specify the amount by which you downsample. You could put this in a
try {} catch {}block and a loop that increments the downsample if you aren’t sure the amount you’ll need.