During runtime, I am trying to put an image in the surface view. When I tried using the image from the Drawable folder I got Out of memory error. After a quick search in the stackoverflow, I found that there will be some relief if we access the image from the asset folder. But still I get the Out of memory error during runtime.
I have analyzed and found that scaling will help in resolving this kind of memory related issues. The thing is that I have the image size of 1280 x 720 and the device size also the same. Hence I feel like the scaling will not have any effect.
As we have experts in this community, I would appreciate if you can help me with some suggestions/examples to resolve this kind of issue.
Scenario 1:
Using the Bitmap from Drawable folder.
backgoundImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.backgroundhomepage), (int) dWidth, (int) dHeight, true);
/***********************************************************************************************************************************************************
1. To get the image from asset library
**************************************************************************************************************************************************************/
public Bitmap getAssetImage(Context context, String filename) throws IOException {
AssetManager assets = context.getResources().getAssets();
InputStream buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png")));
Bitmap bitmap = BitmapFactory.decodeStream(buffer);
return bitmap;
}
Scenario 2:
Using the Bitmap from Assets folder
backgoundImage = Bitmap.createScaledBitmap(getAssetImage(context,"backgroundhomepage"), (int) dWidth, (int) dHeight, true);
You can use the following code to load the bitmap from file:
It should resolve your out of memory exception. Link here has a good detailed explanation of your answer.
Got a quick Solution
put into appplication tag in manifest file.
OutofMemory occurs when your app exceeds memory allocated in heap. The bitmap is too large to fit in memory ie heap. In such a case you run out of memory. You need to scale down the bitmap and then use the same. For that check the link below
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.
There is also a blog @ http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html (avoiding memory leaks)
Quoting from the docs
The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources. Choose the most appropriate decode method based on your image data source. These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception. Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class.
Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.
Also check this link for memory management.
https://www.youtube.com/watch?v=_CruQY55HOk