-1

编辑:我解决了将 UIL 库更新到 1.8.0 的问题,因为从资产文件夹添加图像的功能在早期版本中不可用。我确实错过了。

我正在使用通用图像加载器库在 ListView 中显示图像列表

如果图像不存在,我想从可绘制或资产文件夹加载占位符。

在库文档中,这是接受的 URI:

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

但使用任一选项都会触发java.lang.NullPointerException

这是我加载图像的 onResume 方法:

@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

从文件加载很好。

如果我尝试使用常见的 Android 方法加载占位符图像:

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

然后图像加载正常(即使我在这里提到了另一个问题)

这是日志:

 `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)`

我错过了什么?我是否传递了格式错误的 URI?

4

2 回答 2

5

UIL仅从1.8.0content://版本开始支持和开箱即用的assets://方案。drawable://

于 2013-03-08T13:51:24.093 回答
2

我猜你确实检查过这里使用的uri

imageLoader.displayImage(file_prefix + image_path, image);

形成良好。

我查看BaseImageDownloader了库中关于检索资产文件的方法的类,并且所有代码看起来都井井有条。以下是您发生错误的地方。图像的选项为空,这意味着图像路径不正确。

    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;

使用此代码进行测试,看看是否得到相同的结果。这对我有用,所以它也应该对你有用。如果没有仔细检查您的文件名和路径。如果您在错误发生之前无法找到传递给 ImageDownloader 的 Uri 的答案。

    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);
    }
于 2013-03-07T17:07:33.493 回答