2013-04-24 81 views
0

嗨,我正在製作一個Android應用程序,允許用戶在屏幕上繪圖。我有一個按鈕,當按下時,我希望畫布作爲圖像保存到內部存儲器中。我曾嘗試以下代碼,但我不斷收到FileNotFoundException open failed: EACCES (permission denied)Android - 如何將畫布保存到內存中的圖像

 String path = Environment.getDataDirectory().getAbsolutePath(); 
     File file = new File(path+"image.png"); 

     Bitmap bitmap = Bitmap.createBitmap(100,200,Bitmap.Config.ARGB_8888); 

     FileOutputStream ostream; 

     try { 
      ostream = new FileOutputStream(file); 
      bitmap.compress(CompressFormat.PNG, 100, ostream); 
      ostream.flush(); 
      ostream.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

回答

0

嘗試context.openFileInput(字符串文件名)創建FileOutputStream中。

bitmap.compress(CompressFormat.PNG, 100, context.openFileInput(fileName); 
+0

的類型的方法openFileOutput(字符串,整數)上下文不適用於參數(字符串) – Poensvah 2013-04-24 18:52:57

+0

@Poensvah它是'openFileInput(String)',對於'openFileOutput(String,int)'你想選擇一個模式,它是一個int常數。 – StoneBird 2013-04-24 19:44:31

0
在這部分

試圖改變這樣的:

File file = new File(path + File.separator + "image.png"); 

,並確保你在你的清單中有這些:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />