Android SD Card Write Permission using SAF (Storage Access Framework)

后端 未结 1 1177
一整个雨季
一整个雨季 2020-11-27 14:09

After a lot of findings about how to write(and rename) a file in SD Card (android 5 and above), I think the new SAF provided by android will be required to take permission f

相关标签:
1条回答
  • 2020-11-27 15:05

    Letting the user choose the "SD card" or even the "Internal storage" SAF root give your application access to the corresponding storage, but only through the SAF API, not directly via the filesystem. For example you code could be translated into something like :

    public void writeFile(DocumentFile pickedDir) {
        try {
            DocumentFile file = pickedDir.createFile("image/jpeg", "try2.jpg");
            OutputStream out = getContentResolver().openOutputStream(file.getUri());
            try {
    
                // write the image content
    
            } finally {
                out.close();
            }
    
        } catch (IOException e) {
            throw new RuntimeException("Something went wrong : " + e.getMessage(), e);
        }
    }
    

    In recent version of Android, accessing data outside your application using java.io.File is almost completely deprecated.

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