SAF - 将文件从私有应用程序文件夹复制到授权的 SAF 文件夹时出现 NullPointerException [英] SAF - NullPointerException when copying file from private app folder to authorized SAF folder
问题描述
我的应用程序必须将文件从私有应用程序文件夹复制到授予适当权限的 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.
当DocumentsContract
将Uri
称为文档"时,表示通过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()
创建的 - 来自
FileProvider.getUriForFile()
的 - 由
Uri.parse()
从http
或https
URL 创建的Uri
- 等
Uri
Uri
- A
Uri
created byUri.fromFile()
- A
Uri
fromFileProvider.getUriForFile()
- A
Uri
created byUri.parse()
from anhttp
orhttps
URL - Etc.
这篇关于SAF - 将文件从私有应用程序文件夹复制到授权的 SAF 文件夹时出现 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!