gpt4 book ai didi

java - 如何将Assets文件夹复制到内部存储?

转载 作者:行者123 更新时间:2023-12-02 04:02:11 25 4
gpt4 key购买 nike

我从这里复制了许多可能的解决方案以及我的任何不成功的解决方案。我尝试将 Assets 文件夹复制到存储数据,但在尝试复制 Assets 数据后日志中总是出现错误

private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
Toast.makeText(this, ""+filename, Toast.LENGTH_LONG).show();
try {
in = assetManager.open(filename);
//File outFile = new File(Environment.getExternalStorageDirectory()+"/Android/data/"+getApplicationInfo().packageName+"/", filename);
File outFile = new File(Environment.getExternalStorageDirectory()+"/osmdroid/", filename);
out = new FileOutputStream(outFile);
//out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}

请问您知道这个错误吗?

06-23 18:06:06.316 10803-10803/com.restaurantesencuba.myapplication E/tag: Failed to copy asset file: images
java.io.FileNotFoundException: images

最佳答案

我得到了完全相同的错误,实际上在调试时我发现了“图像”,“声音”和“webkit”,所以似乎你应该跳过不存在的错误,因为没有其他方法来检查它们是否存在存在:

AssetManager assetManager = context.getAssets();
String[] files;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
return;
}
for(String filePath : files) {
try {
InputStream in = assetManager.open(filePath);
//do your work...
} catch (IOException ignored) { }
}

关于java - 如何将Assets文件夹复制到内部存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56728153/

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