我的应用程序想要将文件从私有应用程序文件夹复制到用户选择的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
这确实是在文件系统上发生的事情。实际上,该文件是在父文件夹而非子文件夹中复制和创建的。 这段代码来自SO的答案:
SAF - Invalid URI error from DocumentsContract.createDocument method (FileOutputStream copy)
答案 0 :(得分:0)
我不知道这是否只是一种解决方法,但我是否替换
scikit-learn
使用
String docId = DocumentsContract.getTreeDocumentId(uri);
该方法有效。
String docId = DocumentsContract.getDocumentId(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;
}
文件已正确复制到此文件夹中。