【问题标题】:NullPointerException when trying to load local images with ImageLoader尝试使用 ImageLoader 加载本地图像时出现 NullPointerException
【发布时间】:2012-11-13 07:38:21
【问题描述】:

我正在尝试在一项活动中使用 nostra13/ImageLoader (https://github.com/nostra13/Android-Universal-Image-Loader),该活动允许用户从媒体库中选择图像或使用相机拍摄新图像。当这个动作完成后,我就有了图片的完整路径,格式为“/mnt/sdcard/DCIM/100ANDRO/whatever.jpg”,也就是说保存在sdcard中的图片。当我尝试使用 ImageLoader 加载此图像时,出现以下错误:

11-12 16:05:18.966: E/ImageLoader(12905): null
11-12 16:05:18.966: E/ImageLoader(12905): java.lang.NullPointerException
11-12 16:05:18.966: E/ImageLoader(12905): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.saveImageOnDisc(LoadAndDisplayImageTask.java:239)
11-12 16:05:18.966: E/ImageLoader(12905): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:138)
11-12 16:05:18.966: E/ImageLoader(12905): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:72)
11-12 16:05:18.966: E/ImageLoader(12905): 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:444)
11-12 16:05:18.966: E/ImageLoader(12905): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
11-12 16:05:18.966: E/ImageLoader(12905): 在 java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-12 16:05:18.966: E/ImageLoader(12905): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-12 16:05:18.966: E/ImageLoader(12905): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-12 16:05:18.966: E/ImageLoader(12905): at java.lang.Thread.run(Thread.java:1019)

我并没有真正理解错误,图像的路径不为空,我尝试加载图像的图像视图也不为空。我对清单具有必要的权限,并且能够加载远程图像,但不能加载本地图像。 知道会发生什么吗?

提前谢谢你!

这是我的配置:

 imageLoader = ImageLoader.getInstance();
 DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
 .cacheInMemory()
 .cacheOnDisc()
 .showImageForEmptyUri(R.drawable.image_broken)
 .build();
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
 .memoryCache(new WeakMemoryCache())
 .threadPoolSize(4)
 .defaultDisplayImageOptions(defaultOptions)
 .build();
 imageLoader.init(config);

这就是我尝试将图像加载到图像视图中的方式:

ImageView m_ivAvatar = (ImageView) findViewById(R.id.pdi_image_detail);
imageLoader.displayImage(imagen.getUrl(), m_ivAvatar);

还有xml:

<RelativeLayout 
        android:id="@+id/pdi_header_detail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView 
        android:id="@+id/pdi_image_detail"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="fitCenter"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"/>

    <RelativeLayout 
        android:id="@+id/pdi_detail_action_icons_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true">

        <ImageButton 
            android:id="@+id/pdi_detail_icon_share"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:scaleType="fitCenter"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"/>

        <ImageButton 
            android:id="@+id/pdi_detail_icon_map"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:scaleType="fitCenter"
            android:layout_below="@id/pdi_detalle_icon_share"
            android:layout_alignParentLeft="true"/>

    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

  • 什么是imagen? imagen.getURL() 返回什么?
  • 对不起。 imagen 是一个对象,它表示应用程序内部的图像。它包含一些关于它的信息,包括由 getURL() 方法返回的 url(图像的完整路径)。这个 url 可以是本地路径 /mnt/sdcard/.../image.jpg 或远程 http://.../image.jpg

标签: java android nullpointerexception universal-image-loader


【解决方案1】:

ImageLoader 的本地文件路径应以 file:// 开头。 例如。文件:///mnt/sdcard/...

【讨论】:

  • 非常感谢,知道它有效!真丢人,我没有考虑将 file:// 添加到本地路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多