[英]Reading a file from a Res folder
I have put a file "template.html" inside RAW folder and I want to read it into a InputStream. 我在RAW文件夹中放入了一个文件“ template.html”,我想将其读入InputStream。 But it is returning me null.
但这使我归零。 Can't understand what is wrong in the below code
无法理解以下代码中的错误
e.g. fileName passed as parameter is "res/raw/testtemplate.html"
public String getFile(String fileName) {
InputStream input = this.getClass().getClassLoader().getResourceAsStream(fileName);
return getStringFromInputStream(input);
}
Also, there might be a better solution by putting these files in a particular subfolder and putting it inside Asset folder but then I believe I would need to pass context in AssetManager. 另外,通过将这些文件放在特定的子文件夹中并将其放在Asset文件夹中,可能会有更好的解决方案,但是我认为我需要在AssetManager中传递上下文。 I don't understand that solution, sorry I am new to android development.
我不了解该解决方案,对不起,我是android开发的新手。 Can someone shed some light regarding how this approach can be achieved.
有人可以就如何实现这种方法提供一些启示。
EDIT 编辑
I have started implementing this solution with Assets. 我已经开始使用Assets实施此解决方案。 Below method is supposed to return a string containing the entire text of the file stored as template.html.
下面的方法应该返回一个字符串,其中包含存储为template.html的文件的整个文本。
getFile("template.html") // I am sending extension this time getFile(“ template.html”)//我这次发送扩展名
Problem getting error getAssets() is undefined. 获取错误getAssets()的问题未定义。
public String getFile(String fileName) {
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
String line;
try {
reader = new BufferedReader(new InputStreamReader(getAssets().open(fileName)));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
use this 用这个
new BufferedInputStream(getResources().openRawResource(filepath));
this will return a buffered input stream 这将返回一个缓冲的输入流
The file name should be without extension : 文件名应没有扩展名 :
InputStream ins = getResources().openRawResource(
getResources().getIdentifier("raw/FILENAME_WITHOUT_EXTENSION",
"raw", getPackageName()));
For this purposes uses assets folder: 为此,使用资产文件夹:
assets/ 资产/
This is empty. 这是空的。 You can use it to store raw asset files.
您可以使用它来存储原始资产文件。 Files that you save here are compiled into an .apk file as-is, and the original filename is preserved.
您在此处保存的文件将原样编译为.apk文件,并且保留了原始文件名。 You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager.
您可以使用与使用URI的典型文件系统相同的方式浏览此目录,并使用AssetManager将文件读取为字节流。 For example, this is a good location for textures and game data.
例如,这是纹理和游戏数据的理想位置。
So, you could easy get access at assets with context: context.getAssets()
因此,您可以通过context轻松访问资产:
context.getAssets()
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(context.getAssets().open("filename.txt")));
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.