SAF - 将文件从私有应用程序文件夹复制到授权的 SAF 文件夹时出现 NullPointerException [英] SAF - NullPointerException when copying file from private app folder to authorized SAF folder

本文介绍了SAF - 将文件从私有应用程序文件夹复制到授权的 SAF 文件夹时出现 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序必须将文件从私有应用程序文件夹复制到授予适当权限的 SAF 文件夹.

My app has to copy a file from private app folder to a SAF folder with the suitable permissions granted.

使用的代码是:

static boolean copyFileToTargetFolderWithNewName(Activity activity, String docUri,String targetFolderUri,String newName)
{
    deleteIfExisting(activity,Uri.parse(targetFolderUri),newName);

    File newFile = new File(docUri);
    Uri contentUri = FileProvider.getUriForFile(activity, "com.myappname.fileprovider", newFile);

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

    Log.d("copy",contentUri+" "+targetFolderUri+" "+newName);

    try {
//error here
        Uri newUri=DocumentsContract.copyDocument(resolver,contentUri,Uri.parse(targetFolderUri)); 
        DocumentsContract.renameDocument(resolver,newUri,newName);
        result=true;
    } catch (FileNotFoundException e) {
        result=false;
    }
   return result;
}

日志输出:

D/copy: content://com.myappname.fileprovider/external_files/Android/data/com.myappname.app/files/subfolder/file.txt

D/copy: content://com.myappname.fileprovider/external_files/Android/data/com.myappname.app/files/subfolder/file.txt

content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2FSAFfolder/subfolder

content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2FSAFfolder/subfolder

新名称.txt

怎么了?这是标志问题吗?两个域不兼容复制吗?

What's wrong? Is this a flags problem? Are the two domains not compatible for copying?

推荐答案

您正在尝试复制非文档 Uri (contentUri) 和 copyDocument() 的 JavaDocs 声明第一个参数需要是文档 Uri:

You are trying to copy a non-document Uri (contentUri), and the JavaDocs for copyDocument() state that the first parameter needs to be a document Uri:

带有 Document#FLAG_SUPPORTS_COPY 的文档 此值不能为空.

document with Document#FLAG_SUPPORTS_COPY This value must never be null.

DocumentsContractUri称为文档"时,表示通过DocumentsContract获得的Uri本身.这将包括通过ACTION_OPEN_DOCUMENT 获得的Uri 或通过树DocumentFile 获得的Uri.但是,这不会涵盖:

When DocumentsContract refers to a Uri as a "document", it means a Uri obtained through DocumentsContract itself. That would include a Uri obtained through ACTION_OPEN_DOCUMENT or a Uri obtained through a tree DocumentFile. However, that will not cover:

  • Uri.fromFile()
  • 创建的 Uri
  • 来自 FileProvider.getUriForFile()
  • Uri
  • Uri.parse()httphttps URL 创建的 Uri
  • A Uri created by Uri.fromFile()
  • A Uri from FileProvider.getUriForFile()
  • A Uri created by Uri.parse() from an http or https URL
  • Etc.

这篇关于SAF - 将文件从私有应用程序文件夹复制到授权的 SAF 文件夹时出现 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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