Android: BitmapFactory.decodeStream() out of memory with a 400KB file with 2MB free heap
Android library is not so smart for loading images, so you have to create workarounds for this.
In my tests, Drawable.createFromStream
uses more memory than BitmapFactory.decodeStream
.
You may change the Color scheme to reduce memory (RGB_565), but the image will lose quality too:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
Reference: http://developer.android.com/reference/android/graphics/Bitmap.Config.html
You can also load a scaled image, which will decrease a lot the memory usage, but you have to know your images to not lose too much quality of it.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
Reference: http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
To define the inSampleSize dynamically, you may want to know the image size to take your decision:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(stream, null, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
options.inJustDecodeBounds = false;
// recreate the stream
// make some calculation to define inSampleSize
options.inSampleSize = ?;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
You can customize the inSampleSize according to the screen size of the device. To get the screen size, you can do:
DisplayMetrics metrics = new DisplayMetrics();
((Activity) activity).getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int screenHeight =metrics.heightPixels;
Other tutorials:
- http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
- http://developer.android.com/training/displaying-bitmaps/index.html
BitmapFactory OutOfMemory
Please look at this answer
I experienced same problems on downloading and showing many images with caching in memory. Make sure that no more than some number of images (depending on size) staying in memory.
Android -OutofMemory error- bitmap factory
When image size is big it shows java.lang.OutOfMemoryError
try this solutions ,
1) You can use Android-Universal-Image-Loader Library to set images efficiently, this is very nice solution
OR
2) You can shrink bitmap size before use it...
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) height);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) width);
if(heightRatio > 1 || widthRatio > 1)
{
if(heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
}
else
{
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
Android getting an OutOfMemoryError
The error is self explanatory, your Image or images are too large, try something with a maximum of 10kb. This will help you save the memory. This error is common if you are testing on emulator, If so then go to that specific emulator device on device manager and click edit then Increase the RAM, and also internal memory if necessary and also the heap size. Well as for me I would just do my testing first on a real device, if the same issue persists then I would have to review the size of my images.
Android OutOfMemory Issue
but IDK how to use it
Then I guess you haven't implemented it!
Well, that method will convert a file of yours (saved at SD card, eg) to a resized Bitmap wich can be used as background of an ImageView.
Now, answering to your question, you can use it as I show you below:
Bitmap bitmap = decodeFile(new File(your_string_file_path));
myImageView.setImageBitmap(bitmap);
That's all you have to know, I guess.
Related Topics
Get Database File in /Data/Data on Rooted Device
Android Device Not Shown with Adb
Deleting Android Sms Programmatically
Android Animationdrawable and Knowing When Animation Ends
Detecting When Screen Is Locked
How to Save a Bitmap on Internal Storage
How to Hide Status Bar in Android
Android Emulator: Could Not Get Wglgetextensionsstringarb Error
Upgraded to Sdk 2.3 - Now No Emulators Have Connectivity
How to Change Color and Font on Listview
How to Use Asynctask to Show a Progressdialog While Doing Background Work in Android
Android Studio Rendering Problems
Can't Get Write_Settings Permission
How to Use Web Camera in Android Emulator to Capture a Live Image
Android Studio Gradle Configuration with Name 'Default' Not Found