SAF-文件写在父文件夹中,不在正确的路径中 [英] SAF - File written in parent folder, not in the right path

查看:295
本文介绍了SAF-文件写在父文件夹中,不在正确的路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序希望将文件从私有应用程序文件夹复制到在用户选择的SAF文件夹内创建的SAF文件夹. 可以创建文件夹.

My app wants to copy a file from a private app folder to a SAF folder that was created inside an user-selected SAF folder. Folder creation is Ok.

复制方法是:

public static boolean copyFileToTargetSAFFolder(Context context, String filePath, String targetFolder, String destFileName ) 
{


    Uri uri = Uri.parse(targetFolder);

    String docId = DocumentsContract.getTreeDocumentId(uri);
    Log.d("target folder uri",uri.toString());
    Log.d("target folder id",docId);
    Uri dirUri = DocumentsContract.buildDocumentUriUsingTree(uri, docId );
    Log.d("dir uri",dirUri.toString());
    Uri destUri = null;

    try
    {
        destUri = DocumentsContract.createDocument(context.getContentResolver(), dirUri, "*/*", destFileName);
        Log.d("dest uri",destUri.toString());
    } catch (FileNotFoundException e )
    {
        e.printStackTrace();

        return false;
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = new FileInputStream(filePath);

        os = context.getContentResolver().openOutputStream( destUri, "w");

        byte[] buffer = new byte[1024];

        int length;
        while ((length = is.read(buffer)) > 0)
            os.write(buffer, 0, length);

        is.close();
        os.flush();
        os.close();

        return true;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e)     {
        e.printStackTrace();
    }

    return false;
}

日志为:

target folder uri: content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffoldername/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffoldername%2Fsubfoldername

target folder id: raw:/storage/emulated/0/Download/foldername

dir uri: content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffoldername/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffoldername

dest uri: content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffoldername/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffoldername%2Ffile.txt

这确实是在文件系统上发生的事情.实际上,该文件是在父文件夹而非子文件夹中复制和创建的. 这段代码来自SO的答案:

It is indeed what happens on the filesystem. In fact the file is copied and created in the parent folder, not in the subfolder. This code was from an answer on SO:

SAF-来自DocumentsContract.createDocument方法的无效URI错误( FileOutputStream复制)

推荐答案

我不知道这是否只是一种解决方法,但我是否替换

I do not know if it is only a workaround but if I replace

String docId = DocumentsContract.getTreeDocumentId(uri);

使用

String docId = DocumentsContract.getDocumentId(uri);

该方法有效.

public static boolean copyFileToTargetSAFFolder(Context context, String filePath, String targetFolder, String destFileName ) 
{


    Uri uri = Uri.parse(targetFolder);

    String docId = DocumentsContract.getDocumentId(uri);
    Uri dirUri = DocumentsContract.buildDocumentUriUsingTree(uri, docId );
    Uri destUri = null;

    try
    {
       destUri = DocumentsContract.createDocument(context.getContentResolver(), dirUri, "*/*", destFileName);

    } catch (FileNotFoundException e )
    {
        e.printStackTrace();

        return false;
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = new FileInputStream(filePath);

        os = context.getContentResolver().openOutputStream( destUri, "w");

        byte[] buffer = new byte[1024];

        int length;
        while ((length = is.read(buffer)) > 0)
            os.write(buffer, 0, length);

        is.close();
        os.flush();
        os.close();

        return true;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e)     {
        e.printStackTrace();
    }

    return false;
}

的确 现在日志有

dirUri content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffoldername/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffoldername%2Fsubfoldername

文件已正确复制到此文件夹中.

the file is correctly copied inside this folder.

这篇关于SAF-文件写在父文件夹中,不在正确的路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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