简体   繁体   中英

Permission denied when writing a RandomAccessFile

Edit: My question isn't duplicate of Android permission doesn't work even if I have declared it because i have asked for permission at runtime

Edit2: Writing file into Environment.getexternalstoragedirectory is ok but to sdcard gives that error

Hey I'm trying to write a mp3 file with RandomAccessFile to sdcard and this is my code:

public void save(String newFilename) throws IOException, NotSupportedException {
    if (file.compareTo(new File(newFilename)) == 0) {
        throw new IllegalArgumentException("Save filename same as source filename");
    }
    RandomAccessFile saveFile = new RandomAccessFile(newFilename, "rw");
    try {
        if (hasId3v2Tag()) {
            saveFile.write(id3v2Tag.toBytes());
        }
        saveMpegFrames(saveFile);
        if (hasCustomTag()) {
            saveFile.write(customTag);
        }
        if (hasId3v1Tag()) {
            saveFile.write(id3v1Tag.toBytes());
        }
    } finally {
        saveFile.close();
    }
}

private void saveMpegFrames(RandomAccessFile saveFile) throws IOException {
    int filePos = xingOffset;
    if (filePos < 0) filePos = startOffset;
    if (filePos < 0) return;
    if (endOffset < filePos) return;
    RandomAccessFile randomAccessFile = new RandomAccessFile(this.file.getPath(), "r");
    byte[] bytes = new byte[bufferLength];
    try {
        randomAccessFile.seek(filePos);
        while (true) {
            int bytesRead = randomAccessFile.read(bytes, 0, bufferLength);
            if (filePos + bytesRead <= endOffset) {
                saveFile.write(bytes, 0, bytesRead);
                filePos += bytesRead;
            } else {
                saveFile.write(bytes, 0, endOffset - filePos + 1);
                break;
            }
        }
    } finally {
        randomAccessFile.close();
    }
}

and I'm getting this error in logcat :

java.io.FileNotFoundException: /storage/15D5-14F7/Musics/Music/Dream On.mp3: open failed: EACCES (Permission denied)

but i have Write and Read permission why is this happening?

java.io.FileNotFoundException: /storage/15D5-14F7/Musics/Music/Dream On.mp3: open failed: EACCES (Permission denied)

That appears to be a location on removable storage . You do not have read or write access to arbitrary locations on removable storage on Android 4.4+ devices.

but i have Write and Read permission

You might have read and/or write permission to external storage . External storage is not removable storage.

if you write this permission, you can access the permission. But above marshmallow you have to include how to handle runtime permission.

this is solution link.

https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/

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