【问题标题】:Android - gain priviledges to write to secondary sd card at runtimeAndroid - 在运行时获得写入辅助 sd 卡的权限
【发布时间】:2016-03-14 19:28:09
【问题描述】:
The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external 
storage on a device. Apps must not be allowed to write to secondary external storage 
devices, except in their package-specific directories as allowed by synthesized 
permissions. Restricting writes in this way ensures the system can clean up files
when applications are uninstalled.

但是,我发现在运行时获得写入权限(概念证明:Total Commander)是可能的,使用系统文件资源管理器选择文件夹选择完成后允许写入的文件夹。但是我似乎找不到如何实现这一点的示例。有什么帮助吗?我要引用的文件夹选择的屏幕截图如下所示。

【问题讨论】:

    标签: android android-sdcard android-permissions


    【解决方案1】:

    我想我会为未来的访客回答我自己的问题...

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
    startActivityForResult(intent, 1);
    
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        boolean finish = true;
        boolean selectionWasCorrect = false;
        if(resultCode==RESULT_OK){ 
             //Take persistant permission. This way you can modify the selected folder ever after a device restart.
             Uri treeUri = data.getData();
             int takeFlags = data.getFlags()  & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
             getContentResolver().takePersistableUriPermission(treeUri, takeFlags); 
        }
    }
    

    所有这些都只适用于KIT_KAT及以上

    【讨论】:

    • 请注意,这仅适用于 LOLLIPOP 而不是 KIT_KAT。
    • 仅供参考。这为您提供了对所选目录的永久访问权限。要读取/写入 SD 上的文件,请参阅 stackoverflow.com/questions/36862675/…
    猜你喜欢
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多