SAF - Invalid URI error from DocumentsContract.createDocument method (FileOutputStream copy)

∥☆過路亽.° 提交于 2019-12-04 05:50:24

问题


My app is migrating to SAF or, at least some experimentation is going about.

Now it has to copy a file from private app folder to a SAF folder that was authorized.

The used method is:

 static boolean copyFileToTargetFolderWithNewName(Activity activity, String filePath,String targetFolderUri,String newName)
{
    File file = new File(filePath);

    FileInputStream fis=null;
    Uri docUri=null;
    try {
        fis=new FileInputStream(file);
    } catch (FileNotFoundException e) {
    return false;
    }

    deleteIfExisting(activity,Uri.parse(targetFolderUri),newName);


    ContentResolver resolver = activity.getContentResolver();
    boolean result=false;

    int offset=filePath.lastIndexOf(".");
    String ext="";
    if (offset!=-1) ext=filePath.substring(offset+1);
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    try {
//error here
          docUri=DocumentsContract.createDocument(resolver,Uri.parse(targetFolderUri),mimetype,newName);
    } catch (FileNotFoundException e) {
        result=false;
    }

    try {
        ParcelFileDescriptor pfd=resolver.openFileDescriptor(docUri, "w");
        FileOutputStream fos=new FileOutputStream(pfd.getFileDescriptor());
        int b;
        while  ((b=fis.read()) != -1)
            fos.write(b);       
        fis.close();
        fos.close();
        pfd.close();
        result= true;
        } catch (FileNotFoundException e) {
        result=false;
        } catch (IOException e) {
        result=false;
        }
    return result;
}

I get

java.lang.IllegalArgumentException: Invalid URI: content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Ffolder/subfolder

the folder was created by means of this method:

static public DocumentFile createFolderInFolder(Activity activity,String parentFolderUriString,String folderName)
{
DocumentFile result=null;
ContentResolver contentResolver;
contentResolver = activity.getContentResolver();

Uri parentFolderUri=null;

Uri oldParentUri = Uri.parse(parentFolderUriString);
String id = DocumentsContract.getTreeDocumentId(oldParentUri );

parentFolderUri= DocumentsContract.buildChildDocumentsUriUsingTree(oldParentUri , id);

/*
    String id=DocumentsContract.getTreeDocumentId(Uri.parse(parentFolderUriString));

id=StringUtils.fromLastSlashRight(parentFolderUriString);
parentFolderUri= DocumentsContract.buildTreeDocumentUri(PROVIDER_AUTHORITY,id
        );
*/


DocumentFile parentFolder = DocumentFile.fromTreeUri(activity, parentFolderUri);
result=parentFolder.createDirectory(folderName);

/*try {
   result=DocumentsContract.createDocument(contentResolver,parentFolderUri,DocumentsContract.Document.MIME_TYPE_DIR,folderName);

} catch (FileNotFoundException e) {
    result =null;
}*/

return result;
}

as you can see, a previous version of the creation is commented because it did not work. It was necessary to use DocumentFile, but it seems that there are incompatibilities with DocumentsContract. Am I wrong?

So is SAF broken? Or am I circling around?


回答1:


Following code when called with a full path to a local file and the contentscheme obtained by ACTION_OPEN_DOCUMENT_TREE copies the file to that saf location under the same or a different name that is up to you.

public static boolean copyFileToSafFolder(Context context, String filePath, String rootPath, String destFileName ) {

Uri uri = Uri.parse(rootPath);

String docId = DocumentsContract.getTreeDocumentId(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;
}


来源:https://stackoverflow.com/questions/58148196/saf-invalid-uri-error-from-documentscontract-createdocument-method-fileoutput

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!