【问题标题】:How to handle FileNotFoundException?如何处理 FileNotFoundException?
【发布时间】:2011-09-16 11:43:24
【问题描述】:

在我的应用程序中,我将图像存储在缓存中。但是我使用以下代码得到了以下错误。怎么办,有大神可以帮帮我吗?

例外

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)

代码

{

 ......

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

【问题讨论】:

    标签: android file exception


    【解决方案1】:

    因为它是一个缓存,所以它总是有可能在你获取之前被删除。
    因此,您检查文件是否存在,如果文件存在,则使用它,否则从原始来源获取它。

    例如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-20
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多