Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SAF-写入父文件夹的文件,路径不正确_Android_Copy_Parent_Subdirectory_Storage Access Framework - Fatal编程技术网

Android SAF-写入父文件夹的文件,路径不正确

Android SAF-写入父文件夹的文件,路径不正确,android,copy,parent,subdirectory,storage-access-framework,Android,Copy,Parent,Subdirectory,Storage Access Framework,我的应用程序希望将文件从专用应用程序文件夹复制到用户选择的SAF文件夹中创建的SAF文件夹。 文件夹创建正常 复制方法为: public static boolean copyFileToTargetSAFFolder(Context context, String filePath, String targetFolder, String destFileName ) { Uri uri = Uri.parse(targetFolder); String docId =

我的应用程序希望将文件从专用应用程序文件夹复制到用户选择的SAF文件夹中创建的SAF文件夹。 文件夹创建正常

复制方法为:

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
这确实是在文件系统上发生的事情。事实上,文件是在父文件夹中复制和创建的,而不是在子文件夹中。 该代码来自于对以下内容的回答:


我不知道这是否只是一个解决办法,但我是否需要更换

String docId = DocumentsContract.getTreeDocumentId(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
文件已正确复制到此文件夹中

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