【问题标题】:Not able to open file in Fileoutputstream only in case file is in sd card仅当文件在 sd 卡中时才能在 Fileoutputstream 中打开文件
【发布时间】:2017-09-19 10:09:36
【问题描述】:

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

/storage/3263-6232/piyush/Download/start.mp4:打开失败:EACCES(权限被拒绝)

这是代码:

//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)

【问题讨论】:

  • "我能够以读取模式获取此文件,但无法以 rw 模式打开" - 因为您在 Android 4.4+ 上对removable storage 上的任意文件没有读/写访问权限,这并不奇怪。但是,Stack Overflow 是针对编程问题的,你还没有问过问题。

标签: java android android-sdcard randomaccessfile


【解决方案1】:

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

    private void requestPermission() {

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

    }

还有你的 onCreate :

public void onCreate(){
requestPermission();
}

【讨论】:

    猜你喜欢
    • 2011-08-22
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多