2013-04-24 83 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" />