Question

I have got a kind of gallery in my app with large images. I followed this guide to downscale big images to not run in OOM Exceptions. This works quite well with Android 2.3+. But for some reason it fails in Android 2.2. I figured out that the returned Bitmap is null!?

This is the code:

protected static Bitmap decodeSampledBitmapFromResource (Resources res,
                                                         int resId,
                                                         int reqWidth,
                                                         int reqHeight)
{

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options ();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource (res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize (options,
                                                  reqWidth,
                                                  reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource (res, resId, options);
}

The project is hosted on github if you would like to check for the other code.

Logcat:

03-26 13:57:17.724: E/AndroidRuntime(1100): FATAL EXCEPTION: AsyncTask #2
03-26 13:57:17.724: E/AndroidRuntime(1100): java.lang.RuntimeException: An error occured while executing doInBackground()
03-26 13:57:17.724: E/AndroidRuntime(1100):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at java.lang.Thread.run(Thread.java:1096)
03-26 13:57:17.724: E/AndroidRuntime(1100): Caused by: java.lang.NullPointerException: key == null || value == null
03-26 13:57:17.724: E/AndroidRuntime(1100):     at android.support.v4.util.LruCache.put(LruCache.java:117)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at com.finger.sciencequiz.MainActivity.addBitmapToMemoryCache(MainActivity.java:84)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at com.finger.sciencequiz.BitmapWorkerTask.doInBackground(BitmapWorkerTask.java:46)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at com.finger.sciencequiz.BitmapWorkerTask.doInBackground(BitmapWorkerTask.java:1)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-26 13:57:17.724: E/AndroidRuntime(1100):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-26 13:57:17.724: E/AndroidRuntime(1100):     ... 4 more
Was it helpful?

Solution

I ended up using Picasso. It works quite well for me and is very easy to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top