java - Android:将位图保存到手机存储

标签 java android bitmap

我在将位图保存到手机存储时遇到问题。好像我无法创建新文件。我收到异常 --> java.io.Exception: open failed: EAccess (Permission returned) 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);
    }

最佳答案

在 list 文件中添加此权限

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

查看链接以获取更多信息

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

关于java - Android:将位图保存到手机存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16861753/

相关文章:

java JFrame setContentPanel 然后 JPanel 消失

android - 如何使用 ACTION_DEVICE_STORAGE_LOW 来避免 OutOfMemoryError?

java - Android 数组到 Sharedpreferences 与 Gson

android - 缩放和平移后如何获得相对于 Canvas 的触摸坐标?

android - 如何使用 Google 帐户登录

android - 内存不足 : Heap Size Error after adding 10KB ImageButtons

c++ - QOpenGL : Color Index mode

java - 由于 XML 属性顺序更改而导致数字签名损坏

java - 你如何在 Java 中使用 lambda 表达式对字符串进行排序?

java - 使用 transient 对象创建持久对象