vendredi 27 février 2015

Android BitmapFactory.decodeResource takes too much memory

Welcome, I have question about loading Bitmap (in my case from resources). My code:



public void onClick(View view) {
if (mainButton == view) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test);
}
}


The test.jpg image resolution is 3288 x 4936px. It is jpeg (3,9MB / 48,7MB when uncompressed). While this function work (on my Nexus 7 2013 device), following exception occurs:



java.lang.OutOfMemoryError: Failed to allocate a 259673100 byte allocation with 5222644 free bytes and 184MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:467)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:497)
at pl.jaskol.androidtest.MainActivity.onClick(MainActivity.java:50)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


Why application tries to allocate as much as 248MB? I wrote similar application in Qt for Android with the same image in resources and it works OK.


EDIT:




  1. I can not resize it.




  2. This is very simple application, like hello world. It does nothing else, just loads bitmap from resources.




Aucun commentaire:

Enregistrer un commentaire