gpt4 book ai didi

java - 从/root/storage/emulated/0 复制文件在 android 上失败

转载 作者:行者123 更新时间:2023-11-30 00:35:39 25 4
gpt4 key购买 nike

我正在尝试将文件从设备上的某个位置复制到应用程序文件夹,并使用:

public static void copyFile(final String srcAbsolutePath, final String dstAbsolutePath) throws IOException {
FileInputStream srcFileStream = null;
FileOutputStream dstFileStream = null;
try {
srcFileStream = new FileInputStream(srcAbsolutePath);
dstFileStream = new FileOutputStream(dstAbsolutePath);
byte[] writeBytes = new byte[1024];

int bytesCount;
while ((bytesCount = srcFileStream.read(writeBytes)) > 0) {
dstFileStream.write(writeBytes, 0, bytesCount);
}
} catch (IOException ex) {
Log.e(TAG, "Failed to copy the file from src="+srcAbsolutePath+" to="+dstAbsolutePath, ex);
throw ex;
} finally {
// close the streams
if (srcFileStream != null) {
srcFileStream.close();
}

if (dstFileStream != null) {
dstFileStream.close();
}
}
}

我遇到了这个错误:

Failed to copy the file from src=/root/storage/emulated/0/TestApp/1a5e67e1-c166-4a52-abb4-3de61898e109.pdf to=/data/user/0/com.myapp.package/files/78e6a56b-3141-4024-a3a1-f3f44ccc6dfb.pdf
java.io.FileNotFoundException: /root/storage/emulated/0/TestApp/1a5e67e1-c166-4a52-abb4-3de61898e109.pdf (Permission denied)

我有以下权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我正在使用 https://github.com/spacecowboy/NoNonsense-FilePicker选择文件 (Uri)。

我在 AndroidManifest.xml 中有以下内容

    <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/nnf_provider_paths" />
</provider>

如何修复错误?

最佳答案

关于java - 从/root/storage/emulated/0 复制文件在 android 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43460424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com