2016-04-30 430 views
0

编辑:因为我已经要求允许在运行时我的问题是不重复的Android permission doesn't work even if I have declared it权限被拒绝

EDIT2:写入文件到Environment.getexternalstoragedirectory是确定的,但到SD卡给出了错误

嘿我试着写与RandomAccessFile MP3文件到SD卡,这是我的代码:

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

,我在logcat收到此错误:

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

但我有写入和读取权限为什么会发生这种情况?

+1

谢谢我会这样做,并请原谅我的英语不好@MikeM。 –

+0

参考http://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android – sasikumar

+0

谢谢,但我已经做了所有的@sasikumar –

回答

1
java.io.FileNotFoundException: /storage/15D5-14F7/Musics/Music/Dream On.mp3: open failed: EACCES (Permission denied) 
这似乎是在 removable storage的位置

。您无权读取或写入Android 4.4及更高版本设备上可移动存储上的任意位置。

,但我有写和读权限

,你可能读和/或写权限external storage。外部存储不是可移动存储。

+0

那么我如何才能访问可移动存储?一些应用程序可以像RAR –

+0

那样使用存储访问框架(例如'ACTION_OPEN_DOCUMENT')来允许用户选择可移动存储上的文件。你回来的'Uri'可以和'ContentResolver'和'openInputStream()'和'openOutputStream()'等方法一起使用。 – CommonsWare

+0

非常感谢你能用一些代码编辑你的文章吗? @CommonsWare –