简体   繁体   English

授予 root 或 sd 卡其他文件夹权限后,无权将文件写入子文件夹

[英]No permission to write files to subfolders after granting permission to root or other folder of sd-card

In one step, I will be prompted to pick a folder at external SD card.第一步,系统会提示我在外部 SD 卡上选择一个文件夹。 I do and pick a folder.我这样做并选择一个文件夹。

    public void GetPermission(){
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
        startActivityForResult(intent, 42);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent resultData) {

        if (resultCode != RESULT_OK)
            return;
        Uri treeUri = resultData.getData();
        getContext().getContentResolver().takePersistableUriPermission(treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    }

later I download a zip-file (by asynctask) to picked folder from the Inte.net.稍后我将一个 zip 文件(通过 asynctask)下载到从 Inte.net 中选择的文件夹中。 Just to test I download the file to the picked folder with success through the following routine只是为了测试我通过以下例程成功地将文件下载到选择的文件夹

        uri_ext = Uri.parse(uri_string);
        URLConnection conexion = url_download.openConnection();
        conexion.connect();
        int lenghtOfFile = conexion.getContentLength();
        InputStream input = new BufferedInputStream(url_download.openStream());
        OutputStream output = null;

        DocumentFile pickedDir = DocumentFile.fromTreeUri(the_context, uri_ext);
        DocumentFile newFile = pickedDir.createFile("application/zip", zipname);
        output = the_context.getContentResolver().openOutputStream(newFile.getUri());

        byte data[] = new byte[1024];
        long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress((int) (total * 100 / lenghtOfFile));
            output.write(data, 0, count);
            if (isCancelled()) break;
        }
        output.flush();
        output.close();
        input.close();

after that the file exists in the picked folder.之后该文件存在于所选文件夹中。 The download should not take place in the picked folder, but in a subfolder of the picked folder.下载不应发生在所选文件夹中,而应发生在所选文件夹的子文件夹中。 So I create a subfolder with the following code:所以我用以下代码创建了一个子文件夹:

        DocumentFile new_Dir = DocumentFile.fromTreeUri(context, uri_ext);
        new_Dir.createDirectory("new_subfolder");
        uri_string = uri_string + "%2Fnew_subfolder");
        // uri_string = uri_string + "/new_subfolder"); also checked
        // no other code, nothing else

after that the subfolder is present.之后子文件夹存在。 Now I try to load the zip-file from the inte.net into the new subfolder with the exact same code (except for the uri path from varibale uri_string) from above.现在我尝试使用与上面完全相同的代码(除了来自 varibale uri_string 的 uri 路径)将 zip 文件从 inte.net 加载到新的子文件夹中。 Result: the download to the new subfolder does not work.结果:无法下载到新的子文件夹。

Why?为什么? What am I doing wrong?我究竟做错了什么? What do I have to change?我必须改变什么?

After some tests I found an easy solution:经过一些测试,我找到了一个简单的解决方案:

documentDirectory = DocumentFile.fromTreeUri(the_context, Uri.parse(path));
documentDirectory.createDirectory("new_subfolder");
documentDirectory = documentDirectory.findFile("new_subfolder");

after the last line it then continues with the above code and everything works.在最后一行之后,它继续上面的代码,一切正常。 Maybe it was just too easy.也许这太容易了。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM