【问题标题】:How to handle java.io.FileNotFoundException while loading images from webservice in android在android中从webservice加载图像时如何处理java.io.FileNotFoundException
【发布时间】:2014-04-09 14:37:24
【问题描述】:

我的应用程序中有问题 java.io.FileNotFoundException 错误,在我的应用程序中使用Android-Universal-Image-Loader,用于从 Web 服务加载图像,我知道 Filenotfound exceprion 意味着 url 无效,我需要处理这个异常可以任何人帮助我,如何处理这个错误。

public class TestApp extends Application {
    // private static final String TAG = TFEApplication.class.getSimpleName();

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

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.FIFO).build();

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }
}
public class MainActivity extends Activity {
    private ImageLoader mImageLoader;
    private DisplayImageOptions mDisplayImageoptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDisplayImageoptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher).cacheInMemory(true)
                .cacheOnDisc(true).considerExifParams(true).build();
        mImageLoader = ImageLoader.getInstance();
        setContentView(R.layout.activity_main);

    }

    @Override
    protected void onStart() {

        super.onStart();

        try{
        ImageView testView =(ImageView) findViewById(R.id.imageid);
        mImageLoader.displayImage("http://examle.com",testView, mDisplayImageoptions);
        }catch(Exception execException)
        {
            System.out.println(execException.getMessage());
        }
    }

}

出现错误:

java.io.FileNotFoundException: http://exame.com/text.jpg
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
 com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:111)
    at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:77)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.downloadImage(LoadAndDisplayImageTask.java:319)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryCacheImageOnDisc(LoadAndDisplayImageTask.java:298)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:241)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:141)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:841)

【问题讨论】:

  • 太模糊,无法回答。添加详细信息和代码。
  • 添加一个try catch怎么样
  • 现在检查我已经更新了代码

标签: java android filenotfoundexception universal-image-loader


【解决方案1】:

编辑

所以在查看源代码之后,看起来这是库的一个错误。在LoadAndDisplayImageTask.java 中,tryLoadBitmap() 的第一行是File imageFile = getImageFileInDiscCache()。这就是引发异常的原因。 getImageFileInDiscCache() 需要干净地处理 IOException 以便在找不到缓存的情况下继续处理。不幸的是,我没有看到用.jar 覆盖它的真正简单的方法。您可以从 github 导入项目并自己进行修改。

【讨论】:

  • 但出现类似 FileNotFoundException 的 Unreachable catch 块的错误。 try 语句体中永远不会抛出此异常
  • 那么如何使用 Android-Universal-Image-Loader jar 文件来处理这个问题
  • @Ydder 我能够找到源头。查看我的编辑。
【解决方案2】:

尝试下面的代码并在 onLoadingFailed() 处处理错误:

L.writeLogs(false);    

imageLoader = ImageLoader.getInstance();
imageLoader.loadImage(photo_url, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                   imageView.setImageBitmap(loadedImage);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    super.onLoadingFailed(imageUri, view, failReason);
                    MyLogger.log(TAG, "Handle the error here");
                }
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 2017-07-30
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多