使用 SAF(存储访问框架)的 Android SD 卡写入权限 [英] Android SD Card Write Permission using SAF (Storage Access Framework)

查看:46
本文介绍了使用 SAF(存储访问框架)的 Android SD 卡写入权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量有关如何在 SD 卡(Android 5 及更高版本)中写入(和重命名)文件的发现后,我认为 android 提供的新 SAF 将需要获得用户的许可才能写入 SD 卡文件.

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.

我在这个文件管理器应用程序 ES 文件资源管理器中看到它最初以如下方式获取读写权限,如图所示.

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.

选择sd卡后,授予写入权限.

After selecting sd card, the write permission is granted.

所以以同样的方式我尝试使用 SAF,但重命名文件失败.我的代码:

So in the same way I tried to use SAF, but failed in renaming a file. 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();
    }
}

仍然在运行代码后我得到 EAacces 权限被拒绝.

Still after running the code I get EAacces permission denied.

推荐答案

让用户选择SD 卡"甚至内部存储" SAF 根目录可以让您的应用程序访问相应的存储,但只能通过 SAF API,而不是直接通过文件系统.例如你的代码可以被翻译成类似的东西:

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);
    }
}

在最新版本的 Android 中,几乎完全不推荐使用 java.io.File 访问应用程序外部的数据.

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

这篇关于使用 SAF(存储访问框架)的 Android SD 卡写入权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆