从 /root/storage/emulated/0 复制文件在 android 失败
copy file from /root/storage/emulated/0 fails on android
我正在尝试将文件从设备上的某个位置复制到应用程序文件夹,并使用:
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>
如何修复错误?
终于在 NoNonsense-FilePicker (getFileForUri) 中找到了一个实用程序:
我正在尝试将文件从设备上的某个位置复制到应用程序文件夹,并使用:
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>
如何修复错误?
终于在 NoNonsense-FilePicker (getFileForUri) 中找到了一个实用程序: