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?