【发布时间】:2016-08-20 02:59:22
【问题描述】:
在 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;
}
Edit-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;
}
【问题讨论】:
-
如果您的
find方法不起作用,您应该在问题中包含该方法。 -
@ianhanniballake 也许你没看到兄弟,在代码区域向下滚动你会看到
find()方法发布:-) -
检查您的应用信息 --> 是否拥有权限以确保您拥有。
-
已经检查了很多次,PermissionChecker.checkSelfPermission返回GRANTED,app info --> Permission也被授予。 android6.0只有
DocumentFile可以写外接sdcard?
标签: android android-6.0-marshmallow android-sdcard