Matt
Matt

Reputation: 1037

Android: Saving Bitmap to phone storage

I'm having a problem saving a bitmap to the phone storage. It seems as if I can't create a new file. I get the exception--> java.io.Exception: open failed: EAccess (Permission denied) at java.io.File.createNewFile(File.java:940).

The strFileName that is being used is */storage/sdcard0/SpenSDK/images/testimage_00*

The weird thing is that this code was working in a previous project. I am wondering if anyone has and idea as to why this exception is being thrown.

Here is the code to save to the phone storage as a 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);
    }

Upvotes: 0

Views: 614

Answers (4)

Alexander Semenov
Alexander Semenov

Reputation: 1551

In addition to permission, given in previous answers, you should get path to sdcard by using method

Environment.getExternalStorageDirectory();

and append needed directory to returned path.

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

Add this permission in manifest file

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

Check the link for more information

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

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157487

you need the WRITE_EXTERNAL_STORAGE permission. Also you can get rid of createNewFile. If the file does not exists will be created

Upvotes: 1

Alexander
Alexander

Reputation: 48272

Have you forgotten WRITE_EXTERNAL_STORAGE permission?

Does your hardcoded path really point to the sd card?

Upvotes: 0

Related Questions