如何处理FileNotFoundException异常? [英] How to handle FileNotFoundException?

查看:288
本文介绍了如何处理FileNotFoundException异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我在高速缓冲存储器中存储的图像。但我用下面的code有以下错误。如何处理它,任何人可以帮助我吗?

例外

  16 09-16:56:06.001:DEBUG / WifiService(98):启用并启动无线因updateWifiState
09-16 17:07:36.581:WARN / System.err的(21480):java.io.FileNotFoundException:/mnt/sdcard/Android/data/com.ibkr.elgifto/cache/bitmap_dc9a5b371e3c3915d12d0f32a56075022a505119.tmp(没有这样的文件或目录)
09-16 17:07:36.611:WARN / System.err的(21480):在org.apache.harmony.luni.platform.OSFileSystem.openImpl(本机方法)
09-16 17:07:36.611:WARN / System.err的(21480):在org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
09-16 17:07:36.621:WARN / System.err的(21480):在java.io.FileOutputStream中的<&初始化GT;(FileOutputStream.java:97)。
09-16 17:07:36.621:WARN / System.err的(21480):在java.io.FileOutputStream中的<&初始化GT;(FileOutputStream.java:69)。
09-16 17:07:36.631:WARN / System.err的(21480):在com.ibkr.elgifto.GiftSuggestions $ itemlistadapter $ 4 $ 1.run(GiftSuggestions.java:606)

code

  { ...... 最终文件文件= getCacheFile(图片网址); file.getParentFile()mkdirs()。 file.createNewFile(); ......}      公共文件getCacheFile(字符串URL)
         {
             //首先计算该URL的缓存键和缓存文件路径
             文件求CacheFile = NULL;
             尝试
             {
                 消息摘要mDigest = MessageDigest.getInstance(SHA-1);
                 mDigest.update(url.getBytes());
                 最后弦乐cacheKey = bytesToHexString(mDigest.digest());
                 如果(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
                 {
                     求CacheFile =新的文件(Environment.getExternalStorageDirectory()
                            +文件分割符+机器人
                            +文件分割符+数据
                            +文件分割符+ GiftSuggestions.this.getPackageName()
                            +文件分割符+缓存
                            +文件分割符+bitmap_+ cacheKey +的.tmp);
                 }
             }
             赶上(抛出:NoSuchAlgorithmException E)
             {
                 //哦,SHA-1不可用(怪异),不缓存位图。
             }
             返回求CacheFile;
         }         私人字符串bytesToHexString(字节[]字节)
         {
             // http://stackoverflow.com/questions/332079
             StringBuffer的SB =新的StringBuffer();
             的for(int i = 0; I< bytes.length;我++)
             {
                 十六进制的字符串= Integer.toHexString(0xFF的&安培;字节[I]);
                 如果(hex.length()== 1){
                     sb.append('0');
                 }
                 sb.append(十六进制);
             }
             返回sb.toString();
         }


解决方案

由于它的高速缓存,总有一个机会,你取之前被删除。结果
因此,你检查文件是否存在,如果文件存在,使用它,否则从原始来源获取它。

例如

 文件f =新的文件(路径);
如果(f.exists()){
    //使用文件
}其他{
    //从原始源取
}

In my app i am storing images in the cache memory. But i got the following error using the following code. How to handle it, can anybody help me?

Exception

09-16 16:56:06.001: DEBUG/WifiService(98): enable and start wifi due to updateWifiState
09-16 17:07:36.581: WARN/System.err(21480): java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.ibkr.elgifto/cache/bitmap_dc9a5b371e3c3915d12d0f32a56075022a505119.tmp (No such file or directory)
09-16 17:07:36.611: WARN/System.err(21480):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
09-16 17:07:36.611: WARN/System.err(21480):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
09-16 17:07:36.621: WARN/System.err(21480):     at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
09-16 17:07:36.621: WARN/System.err(21480):     at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
09-16 17:07:36.631: WARN/System.err(21480):     at com.ibkr.elgifto.GiftSuggestions$itemlistadapter$4$1.run(GiftSuggestions.java:606)

Code

{

 ......

 final File file = getCacheFile(imageUrl);

 file.getParentFile().mkdirs(); 

 file.createNewFile();

 ......

}

      public File getCacheFile(String url) 
         {
             // First compute the cache key and cache file path for this URL
             File cacheFile = null;
             try
             {
                 MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
                 mDigest.update(url.getBytes());
                 final String cacheKey = bytesToHexString(mDigest.digest());
                 if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) 
                 {
                     cacheFile = new File(Environment.getExternalStorageDirectory()
                            + File.separator + "Android"
                            + File.separator + "data"
                            + File.separator + GiftSuggestions.this.getPackageName()
                            + File.separator + "cache"
                            + File.separator + "bitmap_" + cacheKey + ".tmp");              
                 }
             }
             catch (NoSuchAlgorithmException e) 
             {
                 // Oh well, SHA-1 not available (weird), don't cache bitmaps.
             }
             return cacheFile;
         }

         private String bytesToHexString(byte[] bytes) 
         {
             // http://stackoverflow.com/questions/332079
             StringBuffer sb = new StringBuffer();
             for (int i = 0; i < bytes.length; i++)
             {
                 String hex = Integer.toHexString(0xFF & bytes[i]);
                 if (hex.length() == 1) {
                     sb.append('0');
                 }
                 sb.append(hex);
             }
             return sb.toString();
         }

解决方案

Since its a cache , there is always a chance that it is deleted before you fetch.
Therefore you check for file existence and if file exists, use it otherwise fetch it from the original source.

e.g.

File f = new File(path);  
if(f.exists()) {  
    //Use the file  
} else {  
    //Fetch from the original source  
}  

这篇关于如何处理FileNotFoundException异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆