Search code examples
androidandroid-drawableuniversal-image-loader

Loading image from drawable or asset folder triggers java.lang.NullPointerException


Edit: I solved this updating the UIL library to 1.8.0, since the feature to add images from the asset folder was not available in earlier releases. I did miss that.

I am displaying a list of images in a ListView using the Universal Image Loader library

If an image is not present, I want to load a placeholder from either the drawable or the asset folder.

On the library docs this are accepted URI:

 String imageUri = "assets://image.png"; // from assets
 String imageUri = "drawable://" + R.drawable.image; // from drawables

but using either option will trigger a java.lang.NullPointerException

Here is my onResume method where I load my images:

@Override
public void onResume() {
    super.onResume();

    //open connection to db
    db = new DBAdapter(this);
    db.open();

    // get all defects for this unit
    defectList = db.getAllDefectsByUnit(unit_id);
    // create an array adapter and let it to display our row
    defects = new SimpleCursorAdapter(this, R.layout.defect_row, defectList, new String[] { "defect", "image_path" }, new int[] { R.id.defect, R.id.image }, 0);

    //set custom view using ViewBinder
    SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

            //int placeholder_id = getResources().getIdentifier("placeholder", "drawable", getPackageName());

            //get column name
            String name = cursor.getColumnName(columnIndex);

            //for the thumbnail column,if we have an image replace the placeholder
            if ("image_path".equals(name)) {

                ImageView image = (ImageView) view.findViewById(R.id.image);
                //Bitmap thumbnail;
                String image_path = cursor.getString(columnIndex);

                Log.i("image_path ->", image_path);

                if (!image_path.equalsIgnoreCase("")) {

                    // Load and display image asynchronously
                    imageLoader.displayImage(file_prefix + image_path, image);

                } else {

                    String imageUri = "drawable://" + R.drawable.placeholder; // drawable
                    //image.setImageResource(placeholder_id);

                    // Load and display image asynchronously
                    imageLoader.displayImage(imageUri, image);

                }

                return true;

            }

            //for the defect column, just add the text to the view
            if ("defect".equals(name)) {

                String defect_text = cursor.getString(columnIndex);

                TextView defect_holder = (TextView) view.findViewById(R.id.defect);
                defect_holder.setText(defect_text);

                return true;
            }

            return false;
        }
    };

    defects.setViewBinder(binder);

    setListAdapter(defects);

}//onResume

Loading from file is fine.

If I try to load the placeholder image using the common Android method:

int placeholder_id = getResources().getIdentifier("placeholder", "drawable", getPackageName());
image.setImageResource(placeholder_id);

then the image is loaded fine (even though I had another issue mentioned here)

Here is the log:

 `03-07 15:58:07.141: E/ImageLoader(22950): null
 03-07 15:58:07.141: E/ImageLoader(22950): java.lang.NullPointerException
 03-07 15:58:07.141: E/ImageLoader(22950):  at com.nostra13.universalimageloader.core.ImageDecoder.computeImageScale(ImageDecoder.java:121)
 03-07 15:58:07.141: E/ImageLoader(22950):  at com.nostra13.universalimageloader.core.ImageDecoder.getBitmapOptionsForImageDecoding(ImageDecoder.java:104)
 03-07 15:58:07.141: E/ImageLoader(22950):  at com.nostra13.universalimageloader.core.ImageDecoder.decode(ImageDecoder.java:82)
 03-07 15:58:07.141: E/ImageLoader(22950):  at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeWithOOMHandling(LoadAndDisplayImageTask.java:252)
 03-07 15:58:07.141: E/ImageLoader(22950):  at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:235)
 03-07 15:58:07.141: E/ImageLoader(22950):  at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:211)
 03-07 15:58:07.141: E/ImageLoader(22950):  at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:128)
 03-07 15:58:07.141: E/ImageLoader(22950):  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
 03-07 15:58:07.141: E/ImageLoader(22950):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 03-07 15:58:07.141: E/ImageLoader(22950):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 03-07 15:58:07.141: E/ImageLoader(22950):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 03-07 15:58:07.141: E/ImageLoader(22950):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 03-07 15:58:07.141: E/ImageLoader(22950):  at java.lang.Thread.run(Thread.java:856)`

What am I missing? Am I passing a malformed URI?


Solution

  • UIL supports content://, assets:// and drawable:// schemes out of the box only since version 1.8.0.