Android-询问写入SD卡权限对话框 [英] Android - ask write to SD card permission dialog

查看:258
本文介绍了Android-询问写入SD卡权限对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得对设备SD卡根目录的写访问权.我可以毫无问题地写入SD卡中的设备内部存储和应用包文件夹.我发现可以使用Storage Access Framework获得对SD卡的访问权限,因此我查看了其他应用程序以了解它们如何获得对SD卡的访问权限,并找到了Total Commander及其简洁的权限对话框.

I am having trouble gaining write access to device SD card root directory. I can write to device internal storage and app package folder in an SD card without problems. I found that gaining access to an SD card is possible using Storage Access Framework, so I looked at other apps to see how they gain access to an SD card, and found Total Commander with its neat permission dialog.

由于找不到任何示例,如何显示此类对话框?我的目标是将大型文件存储在SD卡上,并在卸载该应用程序后保留该文件.

How to show such a dialog because I cannot find any examples? My goal is to store large files on the SD card that stay after uninstalling the app.

类似的问题:

Android(在外部SD-卡):java.io.IOException:权限被拒绝

Android M写入SD卡-权限被拒绝

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

推荐答案

我找到了开源文件管理器.看了一下代码,终于找到了解决方案.

I found open source file manager. Looked at the code and finally found the solution.

仅适用于Android N(7.0.0)(api 24)及更高版本.

Only works on Android N (7.0.0) (api 24) and above.

首先获取SD卡的根路径并显示用户权限对话框.

First get root path of SD card and show user permission dialog.

public void takeCardUriPermission(String sdCardRootPath) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            File sdCard = new File(sdCardRootPath);
            StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
            StorageVolume storageVolume = storageManager.getStorageVolume(sdCard);
            Intent intent = storageVolume.createAccessIntent(null);
            try {
                startActivityForResult(intent, 4010);
            } catch (ActivityNotFoundException e) {
            }
        }
    }

当用户接受请求时,onActivityResult()会被触发,我们可以从意图数据中保存uri

When user accepts the request, onActivityResult() gets triggered and we can save uri from intent data

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 4010) {

            Uri uri = data.getData();

            grantUriPermission(getPackageName(), uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
                    Intent.FLAG_GRANT_READ_URI_PERMISSION);

            final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
                    Intent.FLAG_GRANT_READ_URI_PERMISSION);

            getContentResolver().takePersistableUriPermission(uri, takeFlags);
        }
    }

现在,我们可以检索SD卡根目录的uri并将其与Storage Access Framework一起使用

Now we can retrieve uri of SD card root and use it with Storage Access Framework

public Uri getUri() {
        List<UriPermission> persistedUriPermissions = getContentResolver().getPersistedUriPermissions();
        if (persistedUriPermissions.size() > 0) {
            UriPermission uriPermission = persistedUriPermissions.get(0);
            return uriPermission.getUri();
        }
        return null;
    }

这篇关于Android-询问写入SD卡权限对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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