仅在文件位于SD卡中的情况下才能在Fileoutputstream中打开文件

时间:2017-09-19 10:09:37

标签: java android android-sdcard randomaccessfile

以下是start.mp4文件的SD卡路径。我能够以读取模式获取此文件,但无法在rw模式下打开。我也给了运行时权限。 它引发了一个例外:

  

/storage/3263-6232/piyush/Download/start.mp4:open failed:EACCES(Permission denied)

这是代码:

//This is the sd-card path of the file which needs to be edited

String sdCardPath = "/storage/3263-6232/piyush/Download/start.mp4"; 
File file = new File(sdCardPath );
try{

RandomAccessFile rfs = new RandomAccessFile(file, "rw");

rfs.seek(file.length());
rfs.close();

} catch (IOException e) {
   e.printStackTrace();
}
In the above code I have taken sdcardpath to a file which exists in sdcard. Than after that whenever I tried to open that file in outputstream using RandomAccessFile it gives FilenotFound Exception:

/storage/3263-6232/piyush/Download/start.mp4: open failed: EACCES (Permission denied)

1 个答案:

答案 0 :(得分:0)

如果您使用的是Android 6及更高版本,则应该提出运行时权限请求 那样:

    private void requestPermission() {

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

    }

和你的onCreate:

public void onCreate(){
requestPermission();
}