2016-04-30 259 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 –