Android:从服务器下载图像并将其保存在设备缓存中

时间:2011-11-12 09:54:13

标签: android caching hashmap filenotfoundexception

我已经浏览了以下链接

enter link description here

enter link description here

我按照以下教程在从服务器加载图像后对图像进行缓存

enter link description here

我的代码段是:

private Bitmap getBitmap(String url) {
//      PhotoToLoad photoToLoad = new PhotoToLoad(url, new ImageView(a));
//      String filename = photoToLoad.url;
        //String filename = url;
        String filename = String.valueOf(url.hashCode());
        Log.v("TAG FILE :", filename);
        File f = new File(cacheDir, filename);
        // Is the bitmap in our cache?
        Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
        if (bitmap != null)
            return bitmap;
        else {
            // Nope, have to download it
            try {
                bitmap = BitmapFactory.decodeStream(new URL(url)
                        .openConnection().getInputStream());
                // save bitmap to cache for later
                writeFile(bitmap, f);
                return bitmap;
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
                Log.v("FILE NOT FOUND", "FILE NOT FOUND");
                return null;
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                return null;
            }
        }
    }

    private void writeFile(Bitmap bmp, File f) {
        FileOutputStream out = null;

        try {
            out = new FileOutputStream(f);
            bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (Exception ex) {
            }
        }
    }

我得到FileNotFoundException

11-12 15:19:25.495: VERBOSE/cacheDir(266): /sdcard/data/BalajeeBazaar

11-12 15:19:26.035: VERBOSE/TAG FILE :(266): -951166081

11-12 15:19:26.315: WARN/System.err(266): java.io.FileNotFoundException: /sdcard/data/BalajeeBazaar/-951166081

11-12 15:19:26.315: WARN/System.err(266):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:231)

11-12 15:19:26.315: WARN/System.err(266):     at java.io.FileOutputStream.<init>(FileOutputStream.java:96)

11-12 15:19:26.315: WARN/System.err(266):     at java.io.FileOutputStream.<init>(FileOutputStream.java:69)

11-12 15:19:26.315: WARN/System.err(266):     at com.ecommerce.balajeebazaar.CacheImages.writeFile(CacheImages.java:160)

11-12 15:19:26.315: WARN/System.err(266):     at com.ecommerce.balajeebazaar.CacheImages.getBitmap(CacheImages.java:142)

11-12 15:19:26.315: WARN/System.err(266):     at com.ecommerce.balajeebazaar.CacheImages.access$0(CacheImages.java:125)

11-12 15:19:26.315: WARN/System.err(266):     at com.ecommerce.balajeebazaar.CacheImages$PhotosLoader.run(CacheImages.java:77)

11-12 15:19:26.325: WARN/dalvikvm(266): threadid=17: thread exiting with uncaught exception (group=0x4001aa28)

11-12 15:19:26.325: ERROR/AndroidRuntime(266): Uncaught handler: thread Thread-10 exiting due to uncaught exception

11-12 15:19:26.325: ERROR/AndroidRuntime(266): java.lang.ClassCastException: android.graphics.Bitmap

11-12 15:19:26.325: ERROR/AndroidRuntime(266):     at com.ecommerce.balajeebazaar.CacheImages$PhotosLoader.run(CacheImages.java:79)

请指导我如何解决这个问题?

感谢〜

2 个答案:

答案 0 :(得分:3)

您是否正确定义了目录名称?

也许你没有正确声明目录名......

那么,请你检查一下好吗?

 //Find the dir to save cached images
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"YourDirectoryName");
    else
        cacheDir=context.getCacheDir();
    if(!cacheDir.exists())
        cacheDir.mkdirs();
}

检查你的课程,确保你的路径是正确的或其他任何东西,然后告诉我。

答案 1 :(得分:0)

在堆栈跟踪中,它显示了一个未找到文件的异常。我已经看到当文件夹不存在时会发生这种情况。你先创建它吗?或者,您可以使用上面提到的缓存或外部文件夹。