Can't write to sdcard even with permissions set and external storage in MEDIA_MOUNTED state

前端 未结 3 482
轮回少年
轮回少年 2021-01-13 01:17

While trying to write file to sdcard I get java.io.FileNotFoundException: /filename (Read-only file system) exception. Sadly none of the many solutions posted h

相关标签:
3条回答
  • 2021-01-13 01:38

    Try this:

    File file = new File(filePath);
    File parent = file.getParentFile();
    if (!parent.exists() && !parent.mkdirs()) {
          return;
    }
    
    file = new File(Helpers.StripExtension(filePath) + ".blk");
    OutputStream fos = new FileOutputStream(file);
    fos.write(digest.getBytes());
    fos.close();
    
    0 讨论(0)
  • 2021-01-13 01:42

    Great, I've had the exact same problem some time ago. Path delimiter (or whatever it's called) is missing.

    File file = new File(Environment.getExternalStorageDirectory() + "test_file.blk");
    

    didn't work, but

    File file = new File(Environment.getExternalStorageDirectory() + "/test_file.blk");
    

    performed well

    Also:

    FileWriter f = new FileWriter(Environment.getExternalStorageDirectory().getPath()+ "/foobar");
    f.append("stuff");
    f.close();
    

    Works fine etc.
    I think exceptions said it's readonly, because root filesystem is readonly indeed.

    NOTE:
    java.io.FileNotFoundException: /foobar (Read-only file system) means we are writing to the system root
    java.io.FileNotFoundException: /mnt/sdcard/foobar (Read-only file system) means sdcard root (I didn't get this exception it's just an example)

    tl;dr wrong path

    0 讨论(0)
  • 2021-01-13 02:00

    Try this:

    File file = new File(Helpers.StripExtension(filePath) + ".blk");
    file.createNewFile();
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(digest.getBytes());
    fos.close();
    

    and if that doesn't work, I think you may have a problem when reading from the original file (digest.getBytes()). Post a little more code on this and we can go from there

    Edit:

    If it is not letting you write to the selected directory, try testing with a path like this:

    File file = new File(Environment.getExternalStorageDirectory() + "test_file.blk");
    

    It should work fine and you probably just don't have write permissions to the directory you are using.

    0 讨论(0)
提交回复
热议问题