SAF-来自DocumentsContract.createDocument方法的无效URI错误(FileOutputStream副本) [英] SAF - Invalid URI error from DocumentsContract.createDocument method (FileOutputStream copy)

查看:728
本文介绍了SAF-来自DocumentsContract.createDocument方法的无效URI错误(FileOutputStream副本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在迁移到SAF,或者至少正在进行一些实验.

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

现在,它必须将文件从私有应用程序文件夹复制到已授权的SAF文件夹中.

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

使用的方法是:

 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;
}

我知道

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;
}

如您所见,创建的早期版本被注释掉,因为它不起作用. 必须使用DocumentFile,但似乎与DocumentsContract不兼容.我错了吗?

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?

SAF损坏了吗? 还是我在转转?

So is SAF broken? Or am I circling around?

推荐答案

使用本地文件的完整路径以及ACTION_OPEN_DOCUMENT_TREE获得的contentcheme调用以下代码时,会将文件复制到该saf位置,使用的名称相同或不同.由您决定.

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;
}

这篇关于SAF-来自DocumentsContract.createDocument方法的无效URI错误(FileOutputStream副本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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