0

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?

Community
  • 1
  • 1
Amir_P
  • 8,322
  • 5
  • 43
  • 92

2 Answers2

1
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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • so how can i get access to removable storage? some application do that like RAR – Amir_P Apr 30 '16 at 13:27
  • Use the Storage Access Framework (e.g., `ACTION_OPEN_DOCUMENT`) to allow users to pick files on removable storage. The `Uri` you get back can be used with a `ContentResolver` and methods like `openInputStream()` and `openOutputStream()`. – CommonsWare Apr 30 '16 at 13:32
  • thanks you so much can you edit your post with some code? @CommonsWare – Amir_P Apr 30 '16 at 13:39
0

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/