文件未找到异常访问资源文件。

huangapple go评论72阅读模式
英文:

FileNotFoundException on accessing assets files

问题

我正在尝试将文件从资产文件夹复制到我的应用程序的内部文件目录,但始终抛出 FileNotFoundException 错误:

java.io.FileNotFoundException: android.content.res.AssetManager@f252431/wordsdb.ser: open failed: ENOENT (No such file or directory)
2020-04-05 19:44:48.829 11247-11247/stickyapps.zabanamooz W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:496)
...
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
2020-04-05 19:44:48.836 11247-11247/stickyapps.zabanamooz W/System.err:     at libcore.io.Linux.open(Native Method)
...

我正在使用以下代码:

File file1 = new File(context.getAssets() + "/wordsdb.ser");
File file2 = new File(context.getFilesDir() + "/wordsdb.ser");

try {
    copy(file1, file2);
} catch (IOException e) {
    e.printStackTrace();
}

copy 方法:

public void copy(File src, File dst) throws IOException {
    try (InputStream in = new FileInputStream(src)) {
        try (OutputStream out = new FileOutputStream(dst)) {
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
    }
}

我尝试过重新启动 Android Studio、重新构建项目、重新安装应用程序等等,但仍然没有成功。

英文:

I'm trying to copy a file from assets folder to my app's internal files directory but it keeps throwing FileNotFoundException

j

ava.io.FileNotFoundException: android.content.res.AssetManager@f252431/wordsdb.ser: open failed: ENOENT (No such file or directory)
2020-04-05 19:44:48.829 11247-11247/stickyapps.zabanamooz W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:496)
2020-04-05 19:44:48.829 11247-11247/stickyapps.zabanamooz W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:159)
2020-04-05 19:44:48.829 11247-11247/stickyapps.zabanamooz W/System.err:     at stickyapps.zabanamooz.Managers.WordsManager.copy(WordsManager.java:91)
2020-04-05 19:44:48.830 11247-11247/stickyapps.zabanamooz W/System.err:     at stickyapps.zabanamooz.Managers.WordsManager.<init>(WordsManager.java:46)
2020-04-05 19:44:48.830 11247-11247/stickyapps.zabanamooz W/System.err:     at stickyapps.zabanamooz.MainActivity.onCreate(MainActivity.java:40)
2020-04-05 19:44:48.830 11247-11247/stickyapps.zabanamooz W/System.err:     at android.app.Activity.performCreate(Activity.java:7802)
2020-04-05 19:44:48.830 11247-11247/stickyapps.zabanamooz W/System.err:     at android.app.Activity.performCreate(Activity.java:7791)
2020-04-05 19:44:48.830 11247-11247/stickyapps.zabanamooz W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
2020-04-05 19:44:48.831 11247-11247/stickyapps.zabanamooz W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
2020-04-05 19:44:48.831 11247-11247/stickyapps.zabanamooz W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
2020-04-05 19:44:48.831 11247-11247/stickyapps.zabanamooz W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
2020-04-05 19:44:48.831 11247-11247/stickyapps.zabanamooz W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
2020-04-05 19:44:48.831 11247-11247/stickyapps.zabanamooz W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
2020-04-05 19:44:48.831 11247-11247/stickyapps.zabanamooz W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
2020-04-05 19:44:48.831 11247-11247/stickyapps.zabanamooz W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:107)
2020-04-05 19:44:48.832 11247-11247/stickyapps.zabanamooz W/System.err:     at android.os.Looper.loop(Looper.java:214)
2020-04-05 19:44:48.832 11247-11247/stickyapps.zabanamooz W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7356)
2020-04-05 19:44:48.832 11247-11247/stickyapps.zabanamooz W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2020-04-05 19:44:48.832 11247-11247/stickyapps.zabanamooz W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
2020-04-05 19:44:48.833 11247-11247/stickyapps.zabanamooz W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2020-04-05 19:44:48.836 11247-11247/stickyapps.zabanamooz W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
2020-04-05 19:44:48.836 11247-11247/stickyapps.zabanamooz W/System.err:     at libcore.io.Linux.open(Native Method)

...

I'm using this code:

        File file1 = new File(context.getAssets() + "/wordsdb.ser");
        File file2 = new File(context.getFilesDir() + "/wordsdb.ser");

        try {
            copy(file1,file2);
        } catch (IOException e) {
            e.printStackTrace();
        }

copy method:

public void copy(File src, File dst) throws IOException {
    try (InputStream in = new FileInputStream(src)) {
        try (OutputStream out = new FileOutputStream(dst)) {
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
    }
}

I tried restarting android studio, rebuilding project, reinstalling app and ...
but still no luck.

答案1

得分: 3

尝试使用资源管理器

AssetManager assetManager = getAssets();
String[] files = assetManager.list("");

用于打开特定文件的代码:

InputStream input = assetManager.open(assetName);
英文:

Try using asset manager

AssetManager assetManager = getAssets();
String[] files = assetManager.list("");

This to open a certian file:

InputStream input = assetManager.open(assetName);

huangapple
  • 本文由 发表于 2020年4月5日 02:08:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61032561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定