带有getAssets的Android FileNotFoundException

问题描述

因此目的很简单,我想将文件从APK的资产文件夹复制到设备存储中。我通过下面的代码实现了这一点,并且它实际上在起作用,

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,read);
    }
}

getAssets()。list(“ sourcedir”)实际上列出了APK资产文件夹中sourcedir中的所有图像。

但是,当调用该方法时,它仅对少量文件抛出FileNotFoundException: myfile.png。 例如,我在sourcedir中放置了5个png(1.png,2.png,3.png,myfile.png,zip.png)。复制了所有文件,除了myfile.png

我确认没有任何不同的扩展名或分析过APK,并且所有这5张图片实际上都存在于asset / 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并给出了相同的问题。

更新:

因此,我没有获取所有文件,而是尝试在输入流中添加myfile.png,但这也返回了相同的异常,即使该文件存在于asset / sourcedir文件夹中。

解决方法

找到了解决方案,它也可能对其他人有所帮助。因此,如果要从assets目录的子文件夹中获取多个文件,请确保在调用时包括该子文件夹的名称。就我而言,

in = assetManager.open(filename);

因此添加子文件夹名称和文件名可以解决此问题。

in = assetManager.open("sourcedir/" + filename);

感谢@CommonsWare,他让我知道了这个错误。有趣的是,我从一个StackOverflow的问题中复制了此方法,并对其进行了高投票,并接受了它的答案,但没有人在评论中提及此问题。是的,这是一个简单的错误,我很糟糕。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...