【问题标题】:Android : Download images from server and save them on device cacheAndroid:从服务器下载图像并将它们保存在设备缓存中
【发布时间】:2011-11-12 09:54:13
【问题描述】:

我已经浏览了以下链接

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)

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

谢谢~

【问题讨论】:

标签: android caching hashmap filenotfoundexception


【解决方案1】:

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

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

那么,你能检查一下吗??

 //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();
}

检查你的班级并确保你的路径是正确的或其他任何事情,然后告诉我。

【讨论】:

  • 是的,这是我犯错的地方,谢谢您的回答
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2011-01-30
    • 1970-01-01
    • 2020-05-25
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2011-05-16
    相关资源
    最近更新 更多