Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Bitmap size exceeds VM budget" although function is optimized in Android
    primarykey
    data
    text
    <p>I know, you can find hundreds of questions about this special topic, but I've read most of them and they didn't help. Here's the stack trace:</p> <pre><code>java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:573) at PACKAGE.MyApp.getPhoto(MyApp.java:191) at PACKAGE.MyApp.getEntries(MyApp.java:149) at PACKAGE.AlarmSetup.createNotifications(AlarmSetup.java:48) at PACKAGE.AlarmSetup.onHandleIntent(AlarmSetup.java:30) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:130) at android.os.HandlerThread.run(HandlerThread.java:60) </code></pre> <p>This exception is thrown although I have optimized my Java function (at least I think so) after having read lots of questions here on Stack Overflow. Here's my code:</p> <pre><code>public Bitmap getContactPhoto(String lookup_key) { Uri lookUpUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup_key); Uri contentUri = ContactsContract.Contacts.lookupContact(ctx.getContentResolver(), lookUpUri); try { final int REQUIRED_SIZE = 144; BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; // first get only the size of the image BitmapFactory.decodeStream(ContactsContract.Contacts.openContactPhotoInputStream(ctx.getContentResolver(), contentUri), null, o); int scale = 1; while ((o.outWidth/scale/2) &gt;= REQUIRED_SIZE &amp;&amp; (o.outHeight/scale/2) &gt;= REQUIRED_SIZE) { scale *= 2; } BitmapFactory.Options oScaled = new BitmapFactory.Options(); oScaled.inSampleSize = scale; // decode with calculated sample size now oScaled.inPurgeable = true; // prevent OutOfMemory oScaled.inInputShareable = true; // prevent OutOfMemory return BitmapFactory.decodeStream(ContactsContract.Contacts.openContactPhotoInputStream(ctx.getContentResolver(), contentUri), null, oScaled); } catch (Exception e) { return null; } } </code></pre> <p>Can you help me? I'm becoming desperate, tried a lot of improvements. Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload