我在将位图保存到手机存储时遇到问题。好像我无法创建新文件。我得到了异常--> java.io.Exception: open failed: EAccess (Permission denied) at java.io.File.createNewFile(File.java:940)。
正在使用的 strFileName 是 */storage/sdcard0/SpenSDK/images/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);
}