为什么 android 找不到我的文件?

Why is android unable of finding my file?

我想访问包含在应用程序文件夹中的文件,并且我希望能够在本地访问它。我也不想使用像 android.getresources() 这样的方法,只使用 File file = new File( filePath ); 我的文件保存在 app/src/main/assets/ball.obj

问题是当我执行 File file = new File("app/src/main/assets/ball.obj"); 时,它总是给我一个 FileNotFoundException。我也试过将文件移动到其他地方和绝对路径,但它仍然不起作用。请帮忙。

My file is saved in app/src/main/assets/ball.obj

这是您开发机器上文件的相对路径。 资产不是设备文件系统上的文件。您不能使用 File 访问资产。

要在您的资产上获得 InputStreamcall getAssets() on your Activity or other Context, then call open() on the AssetManager returned by getAssets()

对于 open() 的调用,您需要将 assets/ 内的相对路径传递给您希望读入的资产。因此,如果您在 assets/ 的方法中=13=]、getAssets().open("ball.obj") 会给你一个 InputStream,你可以用它来阅读该特定资产的内容。