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

查看:480
本文介绍了使用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 File Explorer 中看到,它最初以如下所示的方式获得读写许可.

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天全站免登陆