简体   繁体   中英

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


    }

I assume that your Asus Zenfon e 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.

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

而不是使用硬编码目录,尝试使用对Environment.getExternalStorageDirectory的调用,并通过将名称附加到该路径来打开文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM