android6.0应用无法移到sd卡,android - Android 6.0无法写入外部SD卡 - 堆栈内存溢出

在Android v6.0.1中,开发者遇到无法将文件写入外部SDCard的问题。通过使用ACTION_OPEN_DOCUMENT_TREE权限请求,并在onActivityResult中处理URI权限,尝试进行文件复制。然而,复制过程中发现目标文件未在目标目录找到。问题可能在于DocumentFile的查找方法。更新后的findFileInExternal方法通过调整路径处理来寻找正确的文件位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

无法在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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值