我试图将文件夹从资产复制到外部存储,但我有错误消息:
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/appcache/sat/12/CURRENT: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/appcache/sat/12/LOCK: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/appcache/sat/12/MANIFEST-000008: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/assets/here/miami/font/FiraGO-Map.LICENSE: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/assets/here/miami/font/FiraGO_Map.ttf: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/assets/here/miami/font/HanSans_ExtraLight.ttf: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/assets/here/miami/font/Lohit.LICENSE: open failed: ENOENT (No such file or directory)
...
这是我的代码:
private void copyAsset(String path) {
AssetManager manager = getAssets();
// If we have a directory, we make it and recurse. If a file, we copy its
// contents.
try {
String[] contents = manager.list(path);
if (contents == null || contents.length == 0)
throw new IOException();
// Création des dossiers
String destination = Environment.getExternalStorageDirectory().getPath() + File.separator;
File dir = new File( destination + path);
dir.mkdirs();
// Recurse on the contents.
for (String entry : contents) {
copyAsset(path + "/" + entry);
}
} catch (IOException e) {
copyFileAsset(path);
}
}
private void copyFileAsset(String path) {
// File file = new File(Environment.getExternalStorageDirectory().getPath() + "/NTA/" + path);
String destination = Environment.getExternalStorageDirectory().getPath() + File.separator;
File file = new File( destination + path);
try {
InputStream in = getAssets().open(path);
OutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int read = in.read(buffer);
while (read != -1) {
out.write(buffer, 0, read);
read = in.read(buffer);
}
out.close();
in.close();
} catch (IOException e) {
System.out.println("Error :" + e);
}
}
我的仙女名单:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<application
android:requestLegacyExternalStorage="true"
android:name=".ApplicationClass"
android:allowBackup="true"
谢谢
发布于 2022-01-20 12:48:26
实际上,您需要按照以下方式定义路径:
try{
.........
....
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
......
}
https://stackoverflow.com/questions/70784362
复制相似问题