krishna chaitanya
krishna chaitanya

Reputation: 346

open failed EACCES (Permission denied)

i was trying to save some strings to sd card. but it was throwing this exception open failed EACCES (Permission denied). i tested this on a device(asus zenfone 5). i have declared the permissions in the manifest file-WRITE_EXTERNAL_STORAGE, my sd card is mounted and data can be written and read from it(i have personally checked it by copying some mp3 files and playing them). as this was a phone specific app and the phone has an internal and external storage in it and i want to save the file in external sdcard i wrote a code like this. i got the absolute path of external sd card using terminal emulator app by using "cd" and "ls" commands. My phone is not rooted so i cant issue any permission changes directly. I have tried all possible methods possible by google and stackoverflow. please help me out

public void savePublicExternalFile(String data){
        File folder = new File("/Removable/MicroSD/");
        //i have also tried File folder = new File("/Removable/MicroSD");
        File myFile = new File(folder,"mydata.txt");
        WriteData(myFile, data);



    }

    private void WriteData(File myFile, String data){

        if(t){
            Log.i("AbsolutePath",myFile.getAbsolutePath());
            t = false;
        }

        FileOutputStream fileOutputStream=null;
        try {
            fileOutputStream = new FileOutputStream(myFile);
            fileOutputStream.write(data.getBytes());
        } catch (Exception e) {
            Log.i("exception",e.toString());
        }  finally {
            if(fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    Log.i("exception", e.toString());
                }
            }
        }


    }

Upvotes: 3

Views: 2887

Answers (3)

Naveen Mishra
Naveen Mishra

Reputation: 46

I assume that your Asus Zenfone is running on Android versions >= KitKat. So, since KITKAT due to major changes in SD card write policy, you won't be able to write on SD card except on your App Private folder ( which is /storage/../data/com.example/ in SD card).

To access this folder on SD card, there is new API added in KitKat. This API will return an array of Files containing path of all app private folders.

File[] files = ContextCompat.getExternalFilesDirs(context, null)

By default, 0th position path is similar to path returned by older API Environment.getExternalStorageDirectory() and this is primary storage. 1st position (in general) will be other storage directory which is SD card generally. Use this file path to write on SD card. Make sure to verify Mounted state of SD card before writing anything on it.

Hope this help. Let me know if you are facing any other issue.

Upvotes: 3

Yogesh Chuahan
Yogesh Chuahan

Reputation: 358

Few day ago I tried this code for saving an image and it works for me.

void saveFile() {
    String RootDir = Environment.getExternalStorageDirectory()
            + File.separator + "any_folder";

    File myDir = new File(RootDir);
    myDir.mkdirs();

    String fname = "mydata.txt";
    File file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        out.write(data.getBytes());
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    Toast.makeText(AddText.this, "File saved to 'any_folder' folder",
            Toast.LENGTH_LONG).show();
}

Upvotes: 1

user3954436
user3954436

Reputation:

Instead of using an hardcoded directory, try using a call to Environment.getExternalStorageDirectory and open a file by appending the name to that path

Upvotes: 0

Related Questions