简体   繁体   English

使用SAF(存储访问框架)的Android SD卡写权限

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

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 from user to write SD card file. 关于如何在SD卡(android 5及以上版本)中编写(和重命名)文件的大量调查结果后,我认为android提供的新SAF需要获得用户写入SD卡文件的许可。

I have seen in this File Manger Application ES File Explorer that initially it takes read and write permission the following way as shown in pictures. 我在这个文件管理器应用程序ES文件资源管理器中看到,最初它采用以下方式读取和写入权限,如图片所示。

在此输入图像描述

图2

After selecting sd card, the write permission is granted. 选择SD卡后,授予写入权限。

So in the same way I tried to use SAF, but failed in renaming a file. 因此我尝试使用SAF的方式相同,但重命名文件失败了。 My code: 我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rename = (Button) findViewById(R.id.rename);

    startActivityForResult(new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), 42);
}

@Override
public void onActivityResult(int requestCode,int resultCode,Intent resultData) {
    if (resultCode != RESULT_OK)
        return;
    Uri treeUri = resultData.getData();
    DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
    grantUriPermission(getPackageName(), treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    getContentResolver().takePersistableUriPermission(treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}

public void renameclick(View v) {
    File ff = new File("/storage/sdcard1/try1.jpg");
    try {
        ff.createNewFile();
    } catch (Exception e) {
        Log.d("error", "creating");
        e.printStackTrace();
    }
}

Still after running the code I get EAacces permission denied. 在运行代码之后,我仍然拒绝EAacces权限。

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. 让用户选择“SD卡”甚至“内部存储”SAF根目录,使您的应用程序可以访问相应的存储,但只能通过SAF API,而不能直接通过文件系统。 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. 在最新版本的Android中,使用java.io.File访问应用程序外部的数据几乎完全被弃用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SAF(存储访问框架)是否解决了Android 4.4(KitKat)中的SD卡WRITE问题? - Does SAF(Storage Access Framework) solve the SD card WRITE issue in Android 4.4 (KitKat)? Android,Java,使用存储访问框架 (SAF) 以编程方式在 SD 卡上创建目录 - Android, Java, Create directory on SD card programmatically using Storage Access Framework (SAF) 额外启用带存储访问框架(SAF)的显示/隐藏SD卡 - Extra to enable the Show/Hide SD Card with Storage Access Framework (SAF) 如何使用 SAF(存储访问框架)编辑 SD 卡中已存在的文件? - How to edit already present file in SD CARD using SAF(storage access framework)? 使用新的存储访问框架(SAF)将视频录制到外部SD卡时出现IllegalStateException - IllegalStateException when recording a video to an external SD card with the new Storage Access Framework (SAF) Android SAF 可以访问 SD 卡上的特定目录 - Android SAF get access to particular directory on SD Card 在SD卡上写入权限的权限 - Permission to write on SD card android 使用SAF获取持久性SD Cad写权限 - Get Persistable SD Cad write Permission using SAF SAF(存储访问框架)是否需要任何权限才能访问存储? - Does SAF (Storage Access Framework) needs any permission for accessing storage? Android - 获取对 sd 卡上任何文件路径具有写访问权限的 DocumentFile(已获得 sd 卡权限) - Android - get DocumentFile with write access for any file path on sd card (having allready gained sd card permission)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM