How to read file from res/raw by name

AndroidFile IoResources

Android Problem Overview


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

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".

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/ ?

Android Solutions


Solution 1 - Android

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

getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
                             "raw", getPackageName());

To get it as a InputStream

InputStream ins = getResources().openRawResource(
            getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
            "raw", getPackageName()));
    

Solution 2 - Android

Here is example of taking XML file from raw folder:

 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();
	}

Solution 3 - Android

You can read files in raw/res using getResources().openRawResource(R.raw.myfilename).

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.

Solution 4 - Android

Here are two approaches you can read raw resources using Kotlin.

You can get it by getting the resource id. Or, you can use string identifier in which you can programmatically change the filename with incrementation.

Cheers mate 

// R.raw.data_post

this.context.resources.openRawResource(R.raw.data_post)
this.context.resources.getIdentifier("data_post", "raw", this.context.packageName)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBerndView Question on Stackoverflow
Solution 1 - AndroidBerndView Answer on Stackoverflow
Solution 2 - AndroidEmil AdzView Answer on Stackoverflow
Solution 3 - Androiduser2574545View Answer on Stackoverflow
Solution 4 - AndroidMorgan KohView Answer on Stackoverflow