无法在Android v6.0.1中将文件写入外部SDCard。 在设备上测试:Redmi Note3
我已经具有使用以下内容的写许可权:
@TargetApi(21)
public void requestDocumentPermission() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, SDCardBrowser.REQUEST_DOCUMENTS);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_DOCUMENTS && resultCode == RESULT_OK) { // given permission
Uri uri = data.getData();
takeUriPermission(data.getFlags(), uri);
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putString(Constants.PREFS_DEFAULT_URI, uri.toString()).apply();
doCopy(selectedNodes, toNode);
}
super.onActivityResult(requestCode, resultCode, data);
}
这是我下面的复制方法,它可以正常运行,但是我没有在目标字典中找到目标文件。 谁遇到这个问题? 请帮我。
@TargetApi(23)
public static void copyFileV23(File srcFile, File destFile) {
FileInputStream in = null;
OutputStream out = null;
Context context = XXXApplication.getInstance().getApplicationContext();
ContentResolver resolver = context.getContentResolver();
String strUri = PreferenceManager.getDefaultSharedPreferences(context).getString(Constants.PREFS_DEFAULT_URI, null);
if (null == strUri) {
return;
}
DocumentFile rootDocument = DocumentFile.fromTreeUri(context, Uri.parse(strUri));
try {
in = new FileInputStream(srcFile);
DocumentFile target = find(destFile.getAbsolutePath(), rootDocument, getMimeType(srcFile));
out = resolver.openOutputStream(target.getUri());
if (null != out) {
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException ignore) {
}
}
}
private static DocumentFile find(String absolutePath, DocumentFile root, String mime) {
if (null == root || null == absolutePath) {
return null;
}
String[] paths = absolutePath.split("\\/");
for (int i = 0; i < paths.length; i++) {
if (paths[i].equals(""))
continue;
DocumentFile documentFile = root.findFile(paths[i]);
if (null == documentFile) {
if (i < paths.length - 1) {
documentFile = root.createDirectory(paths[i]);
} else {
documentFile = root.createFile(mime, paths[i]);
}
}
root = documentFile;
}
return root;
}
编辑1查找解决方案, find()方法不正确。 如下所示:
private static DocumentFile findFileInExternal(String absolutePath, DocumentFile root, String mime) {
if (null == root || null == absolutePath) {
return null;
}
String externalPath = ExternalStorage.getStoragePath(true);
if (null == externalPath) {
return null;
}
absolutePath = absolutePath.substring(externalPath.length());
String[] paths = absolutePath.split("\\/");
for (int i = 0; i < paths.length; i++) {
if (paths[i].equals(""))
continue;
DocumentFile documentFile = root.findFile(paths[i]);
if (null == documentFile) {
if (i < paths.length - 1) {
documentFile = root.createDirectory(paths[i]);
} else {
documentFile = root.createFile(mime, paths[i]);
}
}
root = documentFile;
}
return root;
}