I want to open a file from the folder res/raw/. I am absolutely sure that the file exists. To open the file I have tried
我想从res / raw /文件夹中打开一个文件。我绝对相信文件存在。要打开我试过的文件
File ddd = new File("res/raw/example.png");
The command
命令
ddd.exists();
yields FALSE. So this method does not work.
收益率为假。所以这种方法不起作用。
Trying
试
MyContext.getAssets().open("example.png");
ends up in an exception with getMessage() "null".
以getMessage()“null”结束异常。
Simply using
简单地使用
R.raw.example
is not possible because the filename is only known during runtime as a string.
因为文件名仅在运行时作为字符串被识别,所以是不可能的。
Why is it so difficult to access a file in the folder /res/raw/ ?
为什么访问文件夹/ res / raw /中的文件如此困难?
3 个解决方案
#1
100
With the help of the given links I was able to solve the problem myself. The correct way is to get the resource ID with
在给定链接的帮助下,我能够自己解决问题。正确的方法是获取资源ID
getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
"raw", getPackageName());
To get it as a InputStream
将其作为InputStream
InputStream ins = getResources().openRawResource(
getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
"raw", getPackageName()));
#2
29
Here is example of taking XML file from raw folder:
以下是从raw文件夹中获取XML文件的示例:
InputStream XmlFileInputStream = getResources().openRawResource(R.raw.taskslists5items); // getting XML
Then you can:
然后你可以:
String sxml = readTextFile(XmlFileInputStream);
when:
什么时候:
public String readTextFile(InputStream inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
return outputStream.toString();
}
#3
9
You can read files in raw/res using getResources().openRawResource(R.raw.myfilename)
.
您可以使用getResources()。openRawResource(R.raw.myfilename)读取raw / res中的文件。
BUT there is an IDE limitation that the file name you use can only contain lower case alphanumeric characters and dot. So file names like XYZ.txt
or my_data.bin
will not be listed in R.
但是有一个IDE限制,您使用的文件名只能包含小写字母数字字符和点。因此,文件名如XYZ.txt或my_data.bin将不会列在R中。
#1
100
With the help of the given links I was able to solve the problem myself. The correct way is to get the resource ID with
在给定链接的帮助下,我能够自己解决问题。正确的方法是获取资源ID
getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
"raw", getPackageName());
To get it as a InputStream
将其作为InputStream
InputStream ins = getResources().openRawResource(
getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
"raw", getPackageName()));
#2
29
Here is example of taking XML file from raw folder:
以下是从raw文件夹中获取XML文件的示例:
InputStream XmlFileInputStream = getResources().openRawResource(R.raw.taskslists5items); // getting XML
Then you can:
然后你可以:
String sxml = readTextFile(XmlFileInputStream);
when:
什么时候:
public String readTextFile(InputStream inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
return outputStream.toString();
}
#3
9
You can read files in raw/res using getResources().openRawResource(R.raw.myfilename)
.
您可以使用getResources()。openRawResource(R.raw.myfilename)读取raw / res中的文件。
BUT there is an IDE limitation that the file name you use can only contain lower case alphanumeric characters and dot. So file names like XYZ.txt
or my_data.bin
will not be listed in R.
但是有一个IDE限制,您使用的文件名只能包含小写字母数字字符和点。因此,文件名如XYZ.txt或my_data.bin将不会列在R中。