0

我在将位图保存到手机存储时遇到问题。好像我无法创建新文件。我得到了异常--> 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);
    }
4

4 回答 4

1

你需要WRITE_EXTERNAL_STORAGE许可。您也可以摆脱 createNewFile。如果文件不存在将被创建

于 2013-05-31T16:24:26.660 回答
1

在清单文件中添加此权限

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

检查链接以获取更多信息

http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE

于 2013-05-31T16:25:55.937 回答
1

除了前面的答案中给出的权限之外,您还应该使用方法获取 sdcard 的路径

Environment.getExternalStorageDirectory();

并将所需的目录附加到返回的路径。

于 2013-05-31T16:32:28.727 回答
0

你忘记WRITE_EXTERNAL_STORAGE许可了吗?

您的硬编码路径是否真的指向 sd 卡?

于 2013-05-31T16:23:45.090 回答