Android - 获得在运行时写入辅助 SD 卡的权限

Android - gain priviledges to write to secondary sd card at runtime

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)使用系统文件资源管理器 select 允许在文件夹 [ 之后写入的文件夹=17=]离子完成。但是我似乎找不到如何实现这个的例子。有什么帮助吗?我正在引用的文件夹 selection 的屏幕截图如下所示。

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

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 及以上