2011-07-11 96 views
1

我想从我的应用程序中的drawables共享图像。我不断收到java.io.FileNotFoundException:/abc.jpg (read-only file system)Android图像发送意图

我的代码

private String SaveCache(int resID) { 
      String path = ""; 
      try { 
       InputStream is = getResources().openRawResource(resID); 
       File cacheDir = this.getExternalCacheDir(); 
       File downloadingMediaFile = new File(cacheDir, "abc.jpg"); 
       byte[] buf = new byte[256]; 
       FileOutputStream out = new FileOutputStream(downloadingMediaFile); 
       while (true) { 
        int rd = is.read(buf, 0, 256); 
        if (rd == -1 || rd == 0) 
         break; 
        out.write(buf, 0, rd);    
       } 
       is.close(); 
       out.close(); 
       return downloadingMediaFile.getPath(); 
      } catch (Exception ex) { 
       Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show(); 
       ex.printStackTrace(); 
      } 
      return path; 
     } 



     private void ImageShare() { 
      String path = SaveCache(R.drawable.testsend); 
      Toast.makeText(this, path, Toast.LENGTH_LONG).show(); 
      Intent share = new Intent(android.content.Intent.ACTION_SEND); 
      share.setType("image/jpeg"); 
      share.putExtra(Intent.EXTRA_SUBJECT, "test"); 
      share.putExtra(Intent.EXTRA_TEXT, "test"); 
      share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + path));  
      try { 
       startActivity(Intent.createChooser(share, "Choose share method.")); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 

     } 

任何帮助表示赞赏,如果有更多的信息需要,我会很乐意提供。

回答

2

确保安装了外部存储器。

getExternalCacheDir()文档:

返回目录控股应用程序缓存文件上 外部存储的路径。如果外部存储当前未装入 ,则返回null,因此无法确保路径存在;当它可用时,您需要再次调用 这个方法。

+4

当你花一个小时捣乱键盘找出这个愚蠢的小错误时,你不觉得讨厌它吗?在检查外部存储的过程中,我发现我忘了添加外部存储的权限。 – zaid