Java Android FileNotFoundException与getAssets
因此,目的很简单,我想将文件从APK的资产文件夹复制到设备存储器。我用下面的代码实现了这一点,而且它确实起了作用Java Android FileNotFoundException与getAssets,java,android,android-assets,Java,Android,Android Assets,因此,目的很简单,我想将文件从APK的资产文件夹复制到设备存储器。我用下面的代码实现了这一点,而且它确实起了作用 public void copyAssets() { AssetManager assetManager = getAssets(); String[] files = null; try { files = getAssets().list("sourcedir"); } catch (IOException e)
public void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = getAssets().list("sourcedir");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "TargetFol");
if (!folder.exists()) {
folder.mkdirs();
}
File outFile = new File(folder, filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}
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);
}
}
getAssets().list(“sourcedir”)实际上列出了APK的assets文件夹中sourcedir中的所有图像
但是当调用该方法时,它仅对少数文件抛出FileNotFoundException:myfile.png
。
例如,我将5个png放在sourcedir(1.png、2.png、3.png、myfile.png、zip.png)中,除了myfile.png之外,所有文件都复制了
我验证了没有不同的扩展,或者我已经分析了APK,所有这5个图像实际上都在assets/sourcedir中
LOGCAT:
E/tag: Failed to copy asset file: myfile.png
java.io.FileNotFoundException: myfile.png
at android.content.res.AssetManager.nativeOpenAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:833)
at android.content.res.AssetManager.open(AssetManager.java:810)
at org.pac.pac.act.copyAssets(act.java:97)
at org.pac.pac.act$2.run(act.java:78)
at java.lang.Thread.run(Thread.java:919)
2020-10-04 21:06:13.189 30767-30999/packagename E/tag: Failed to copy asset file: myfile2.png
java.io.FileNotFoundException: myfile2.png
at android.content.res.AssetManager.nativeOpenAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:833)
at android.content.res.AssetManager.open(AssetManager.java:810)
at org.pac.pac.act.copyAssets(act.java:97)
at org.pac.pac.act$2.run(act.java:78)
我添加了另一个名为myfile2.PNG的PNG,并给出了相同的问题
更新:
E/tag: Failed to copy asset file: myfile.png
java.io.FileNotFoundException: myfile.png
at android.content.res.AssetManager.nativeOpenAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:833)
at android.content.res.AssetManager.open(AssetManager.java:810)
at org.pac.pac.act.copyAssets(act.java:97)
at org.pac.pac.act$2.run(act.java:78)
at java.lang.Thread.run(Thread.java:919)
2020-10-04 21:06:13.189 30767-30999/packagename E/tag: Failed to copy asset file: myfile2.png
java.io.FileNotFoundException: myfile2.png
at android.content.res.AssetManager.nativeOpenAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:833)
at android.content.res.AssetManager.open(AssetManager.java:810)
at org.pac.pac.act.copyAssets(act.java:97)
at org.pac.pac.act$2.run(act.java:78)
因此,我没有获取所有文件,而是尝试在inputstream中添加myfile.png,但这也会返回相同的异常,即使该文件存在于assets/sourcedir文件夹中。找到了解决方案,它可能也会帮助其他人。因此,如果要从assets目录的子文件夹中获取多个文件,请确保在调用时包含子文件夹名称。就我而言
in = assetManager.open(filename);
因此,将子文件夹名称与文件名一起添加解决了这个问题
in = assetManager.open("sourcedir/" + filename);
感谢@commonware让我知道了这个错误。有趣的是,我从一个StackOverflow的问题中复制了这个方法,并以很高的得票率接受了这个答案,但没有人在评论中提到这个问题。是的,这是一个简单的错误,我的错。我建议您编辑您的问题,并为崩溃提供完整的堆栈跟踪。此外,我实际上已使用try-catch处理了该异常,因此没有崩溃,但问题是一样的。@commonware使用最新信息编辑了问题,我预计它们都会失败<代码>文件名是裸文件名<
AssetManager
上的code>open()采用资产内的相对路径。在open()
调用中缺少“sourcedir”
。