Open failed EACCES

I was trying to save some lines to the SD card. but it was throwing this exception open with EACCES (Permission denied) error. 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 personally verified it by copying some mp3 files and playing them back). since it was a phone specific app and the phone has internal and external storage in it and I want to save the file to an external SD card I wrote code like this. I got the absolute path of an external SD card using a terminal emulator application using the "cd" and "ls" commands. My phone is not rooted so I cannot directly change the permissions. I have tried all possible google and stackoverflow methods. you are welcome,help me

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());
                }
            }
        }


    }

      

+3


source to share


3 answers


I am assuming your Asus Zenfone is running in versions Android

>= KitKat

. So, since KITKAT due to significant changes in the SD cards write policy, you will not be able to write to the SD card except your application folder ( /storage/../data/com.example/

on the SD card).

To access this folder on SD card, KitKat

a new one is added API

. This API will return an array of files containing the path for all of the personal folders in the application.

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

      



By default, the 0th

position path is similar to the path returned by the old API Environment.getExternalStorageDirectory()

and this is the primary store. 1st

position (in general) will be another storage directory, which is usually an SD card. Use this path to write to SD card. Make sure you have confirmed the installed state of the SD card before you write anything to it.

Hope for this help. Let me know if you encounter any other problem.

+3


source


Several days ago I tried this code to save 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();
}

      

+1


source


Instead of using a hardcoded directory, try using a call Environment.getExternalStorageDirectory

and open the file by appending the name to that path

0


source







All Articles