I've been strugling with this problem for a while now. Still, I have some hope left.
One of my activities downloads an image by using an AsyncTask. It saves the image in a Bitmap and then displays it in an Image View. This is the code:
@Override
protected String doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(url.openConnection()
.getInputStream(), null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 270, 173);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
options.inPurgeable = true;
options.inInputShareable = true;
bmp = BitmapFactory.decodeStream(url.openConnection()
.getInputStream(), null, options);
} catch (Exception e) {
Log.e(MainActivity.class.toString(),
"No se pudo descargar la imagen");
}
return "";
}
@Override
protected void onPostExecute(String result) {
if (bmp != null) {
imagenNoticia.setImageBitmap(bmp);
imagenNoticia.setVisibility(View.VISIBLE);
}
}
}
But after navigating through the app for a while, by opening and closing this activity, I get an OutOfMemoryError right at the line of code BitmapFactory.decodeStream. I'm sure this activity never duplicates, so I just one of its type at a time.
As you guys can see, I've used several of the best practices about eficiently decoding bitmaps. Like downsampling the received images (inJustDecodeBounds).
I would like to know what else I can do to avoid this error. Does anybody have an idea about it?
Next comes the error.
Thanks! Any help would be appreciate it.
12-16 18:19:17.686: E/AndroidRuntime(13321): FATAL EXCEPTION: AsyncTask #1
12-16 18:19:17.686: E/AndroidRuntime(13321): java.lang.RuntimeException: An error occured while executing doInBackground()
12-16 18:19:17.686: E/AndroidRuntime(13321): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-16 18:19:17.686: E/AndroidRuntime(13321): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-16 18:19:17.686: E/AndroidRuntime(13321): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-16 18:19:17.686: E/AndroidRuntime(13321): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-16 18:19:17.686: E/AndroidRuntime(13321): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-16 18:19:17.686: E/AndroidRuntime(13321): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-16 18:19:17.686: E/AndroidRuntime(13321): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-16 18:19:17.686: E/AndroidRuntime(13321): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-16 18:19:17.686: E/AndroidRuntime(13321): at java.lang.Thread.run(Thread.java:856)
12-16 18:19:17.686: E/AndroidRuntime(13321): Caused by: java.lang.OutOfMemoryError
12-16 18:19:17.686: E/AndroidRuntime(13321): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-16 18:19:17.686: E/AndroidRuntime(13321): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
12-16 18:19:17.686: E/AndroidRuntime(13321): at com.mobilemedianet.larepublica.activity.NotiDetalle$CargarNoticias.doInBackground(NotiDetalle.java:496)
12-16 18:19:17.686: E/AndroidRuntime(13321): at com.mobilemedianet.larepublica.activity.NotiDetalle$CargarNoticias.doInBackground(NotiDetalle.java:1)
12-16 18:19:17.686: E/AndroidRuntime(13321): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-16 18:19:17.686: E/AndroidRuntime(13321): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-16 18:19:17.686: E/AndroidRuntime(13321): ... 5 more