2011-07-11 98 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