使用Universal Image Loader的FileNotFoundException

时间:2013-01-13 16:54:23

标签: java android universal-image-loader

这个问题是关于这个流行的Android库,适用于Android的通用图片加载器https://github.com/nostra13/Android-Universal-Image-Loader

  1. 我确定已添加WRITE_EXTERNAL_STORAGE。
  2. 我启用了内存和磁盘缓存
  3. 我使用UnlimitedDiscCache进行磁盘缓存
  4. 我正在构建Android 2.2 SDK。当我在Android 4.1.2 Nexus S设备上测试应用程序时,会发现例外情况。
  5. 对于我的大多数图像,加载都很平滑。但是,对于某些图像,我总是得到相同的例外。以下是一个例子。
  6. 图片网址:http://i10.topit.me/l046/10046137034b1c0db0.jpg

    回溯:

    01-14 00:24:42.125: 
    
    ERROR/ImageLoader(1671): http://i10.topit.me/l046/10046137034b1c0db0.jpg
    
    java.io.FileNotFoundException: http://i10.topit.me/l046/10046137034b1c0db0.jpg
    
    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
    
    at com.nostra13.universalimageloader.core.download.URLConnectionImageDownloader.getStreamFromNetwork(URLConnectionImageDownloader.java:40)
    
    at com.nostra13.universalimageloader.core.download.ImageDownloader.getStream(ImageDownloader.java:27)
    
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.saveImageOnDisc(LoadAndDisplayImageTask.java:296)
    
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:204)
    
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:128)
    
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
    

    请帮忙。

4 个答案:

答案 0 :(得分:12)

跟进:

我终于在不改变一行服务器代码的情况下解决了我的问题。我所做的是用 HttpClientImageDownloader

替换默认的 URLConnectionImageDownloader

代码示例:

        HttpParams params = new BasicHttpParams();
        // Turn off stale checking. Our connections break all the time anyway,
        // and it's not worth it to pay the penalty of checking every time.
        HttpConnectionParams.setStaleCheckingEnabled(params, false);
        // Default connection and socket timeout of 10 seconds. Tweak to taste.
        HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
        HttpConnectionParams.setSoTimeout(params, 10 * 1000);
        HttpConnectionParams.setSocketBufferSize(params, 8192);

        // Don't handle redirects -- return them to the caller. Our code
        // often wants to re-POST after a redirect, which we must do ourselves.
        HttpClientParams.setRedirecting(params, false);
        // Set the specified user agent and register standard protocols.
        HttpProtocolParams.setUserAgent(params, "some_randome_user_agent");
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);


        ImageLoaderConfiguration config =
                new ImageLoaderConfiguration
                        .Builder(MainActivity.sharedMainActivity.getApplicationContext())
                        .defaultDisplayImageOptions(defaultOptions)
                        .discCache(new UnlimitedDiscCache(cacheDir))
                        .threadPoolSize(1)
                        .memoryCache(new WeakMemoryCache())
                        .imageDownloader(new HttpClientImageDownloader(new DefaultHttpClient(manager, params)))
                        .build();

:)

答案 1 :(得分:4)

寻找libcore.net.http.HttpURLConnectionImpl.getInputStream()

的实施
 @Override
    public InputStream getInputStream() throws IOException {
        if (!doInput) {
            throw new ProtocolException("This protocol does not support input");
        }

        retrieveResponse();

        /*
         * if the requested file does not exist, throw an exception formerly the
         * Error page from the server was returned if the requested file was
         * text/html this has changed to return FileNotFoundException for all
         * file types
         */
        if (responseCode >= HTTP_BAD_REQUEST) {
            throw new FileNotFoundException(url.toString());
        }

        if (responseBodyIn == null) {
            throw new IOException("No response body exists; responseCode=" + responseCode);
        }

        return responseBodyIn;
    }

根据这一点,即使发生服务器错误或未经授权,它也可能抛出FileNotFoundException。

我没有找到HTTP_BAD_REQUEST的值,但它始终是400.源代码链接 http://www.java2s.com/Open-Source/Android/android-core/platform-libcore/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnectionImpl.java.htm

答案 2 :(得分:0)

您唯一能做的就是L.disableLogging()完全禁用日志记录。或根据您的需要更改来源。 资料来源:nostra13's answer for this problem

答案 3 :(得分:0)

此配置调整是自定义的。您可以调整每个选项,可以调整其中一些选项,也可以通过ImageLoaderConfiguration.createDefault(this);方法创建默认配置。

 public static void initImageLoader(Context context) {

    ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
    config.threadPriority(Thread.NORM_PRIORITY - 2);
    config.denyCacheImageMultipleSizesInMemory();
    config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
    config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
    config.tasksProcessingOrder(QueueProcessingType.LIFO);
    config.writeDebugLogs(); // Remove for release app

    // Initialize ImageLoader with configuration.
    ImageLoader.getInstance().init(config.build());
}

我希望这对任何人都有帮助。