gpt4 book ai didi

android - 如何从 asset/raw 目录访问文件

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:54:17 25 4
gpt4 key购买 nike

使用下面的代码,我试图访问存储在 asset/raw 文件夹中的文件,但得到的是 null

E/ERR: file:/android_asset/raw/default_book.txt (No such file or directory)

错误,我的代码是:

private void implementingDefaultBook() {
String filePath = Uri.parse("file:///android_asset/raw/default_book.txt").toString();
File file = new File(filePath);
try {
FileInputStream stream = new FileInputStream(file);
} catch (Exception e) {
e.printStackTrace();
Log.e("ERR ", e.getMessage());
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
}

enter image description here

最佳答案

将您的文本文件放在 Android 项目下的/assets 目录中,然后使用 AssetManager 类访问它。

AssetManager am = context.getAssets();
InputStream is = am.open("default_book.txt");

或者你也可以将文件放在/res/raw目录下,从这里可以通过id访问文件,如下所示

InputStream is = 
context.getResources().openRawResource(R.raw.default_book);

关于android - 如何从 asset/raw 目录访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45908648/

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