-1

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?

Community
  • 1
  • 1
Paranoid Android
  • 4,672
  • 11
  • 54
  • 73

2 Answers2

5

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

nostra13
  • 12,377
  • 3
  • 33
  • 43
  • Please see this questions: http://stackoverflow.com/questions/18034576/images-not-loading-from-assets-folder-using-universal-image-loader – marienke Aug 03 '13 at 15:57
2

I'm guessing you did check to see that the uri used here

imageLoader.displayImage(file_prefix + image_path, image);

is well formed.

I looked through the BaseImageDownloader class in the library on the method where asset files are retrieved and all the code looks to be in order. Below is where your error occurs. Options for the image is null which means the image path is incorrect.

    Options options = new Options();
    options.inJustDecodeBounds = true;
    InputStream imageStream = imageDownloader.getStream(imageUri, displayOptions.getExtraForDownloader());
    try {
        BitmapFactory.decodeStream(imageStream, null, options);
    } finally {
        IoUtils.closeSilently(imageStream);
    }

    int scale = 1;
    int imageWidth = options.outWidth; //<-- this is where your error ocurrs
    int imageHeight = options.outHeight;

Do a test using this code and see if you get the same results. This is working for me so it should also work for you. If not double check your file names and your paths. If you can't find the answer post your Uri that is passed to the ImageDownloader before the error occurs.

    AssetManager mngr = getAssets();
    InputStream stream;
    try {
        stream = mngr.open("your_picture.png");

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = 2;// If you want to scale

        Bitmap bitmap = BitmapFactory.decodeStream(stream, new Rect(), o2);

        ImageView image = (ImageView) view.findViewById(R.id.image);
        image.setImageBitmap(bitmap);

    } catch (IOException e) {
        Log.e(TAG, "ERROR GETTING IMAGE", e);
    }
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
  • I am an idiot...the feature to load images from the asset folder was added in version 1.8.0 of the library, and I was using 1.7.0 instead. I updated the library and everything works as expected. – Paranoid Android Mar 08 '13 at 11:51
  • I voted you up anyway since I might need the code you suggested in another project;) – Paranoid Android Mar 08 '13 at 11:56