我在保存位圖到手機存儲時遇到問題。看起來好像我無法創建新文件。我得到異常 - >java.io.Exception:打開失敗:EAccess(權限被拒絕)在java.io.File.createNewFile(File.java:940)。Android:將位圖保存到手機存儲
正在使用的strFileName是* /存儲/ sdcard0/SpenSDK /圖片/ testimage_00 *
奇怪的是,這個代碼是在以前的項目工作。我想知道是否有人有想法爲什麼拋出這個異常。
以下是將代碼保存爲手機存儲爲PNG的代碼。
private boolean saveBitmapPNG(String strFileName, Bitmap bitmap){
if(strFileName==null || bitmap==null){
System.out.println("!!!error saving image - false in SavePNG - NAME OR BITMAP");
return false;
}
boolean bSuccess1 = false;
boolean bSuccess2;
boolean bSuccess3;
File saveFile = new File(strFileName);
if(saveFile.exists()) {
if(!saveFile.delete())
System.out.println("!!!error saving image - false in SavePNG - could not delete existing");
return false;
}
try {
bSuccess1 = saveFile.createNewFile();//----------------->EXCEPTION IS THROWN HERE
} catch (IOException e1) {
System.out.println("!!!error saving image - false in SavePNG - could not create new file");
e1.printStackTrace();
}
OutputStream out = null;
try {
out = new FileOutputStream(saveFile);
bSuccess2 = bitmap.compress(CompressFormat.PNG, 100, out); //----------------->EXCEPTION IS THROWN HERE
} catch (Exception e) {
e.printStackTrace();
System.out.println("!!!error saving image - false in SavePNG - could not compress");//-->3
bSuccess2 = false;
}
try {
if(out!=null) //----------------->OUT == null here
{
out.flush();
out.close();
bSuccess3 = true;
}
else
System.out.println("!!!error saving image - false in SavePNG - could not close");//-->4
bSuccess3 = false;
} catch (IOException e) {
e.printStackTrace();
System.out.println("!!!error saving image - false in SavePNG - could not close (exception");
bSuccess3 = false;
}finally
{
if(out != null)
{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return (bSuccess1 && bSuccess2 && bSuccess3);
}
感謝您的快速響應!我不能相信我忘了更新清單。 – Matt
@Matt歡迎您。 – Raghunandan