100

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

kgandroid
  • 5,507
  • 5
  • 39
  • 69
Bernd
  • 3,405
  • 3
  • 18
  • 21
  • possible duplicate of [Dynamic Resource Loading Android](http://stackoverflow.com/questions/3648942/dynamic-resource-loading-android) – Simon Apr 09 '13 at 21:27
  • Why is it only known at runtime as a string? Could you keep a mapping between the strings and the IDs? – kabuko Apr 09 '13 at 21:28
  • Read the docs: http://developer.android.com/guide/topics/resources/providing-resources.html – dmon Apr 09 '13 at 21:30
  • Thank you for the links. I will give a correct solution as a separate answer. – Bernd Apr 10 '13 at 19:13

4 Answers4

176

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()));
quietmint
  • 13,885
  • 6
  • 48
  • 73
Bernd
  • 3,405
  • 3
  • 18
  • 21
  • 18
    Just to clarify: Do **not** prepend `raw/` for the file name. Valid example: `.getIdentifier("products","raw", getPackageName());` for `res/raw/products.json` – JJD Jan 24 '14 at 11:16
  • 5
    As stated by JJD, you don't need the raw/ before the FILENAME_WITHOUT_EXTENSION. I found that if you wish to use the extension, for example I have two files abc.jpg and abc.zip, I will add them both to raw folder as abc_jpg and abc_zip (extensions in raw folder are ignored), then assign String variable filename = abc.jpg or abc.zip, then use InputStream ins = getResources().openRawResource(getResources().getIdentifier(filename.replace(".", "_"), "raw", getPackageName())); Hope this helps someone :) – Bruce Jun 28 '14 at 02:38
  • remember to close any streams you open – Pierre Aug 15 '17 at 20:03
  • i need the filepath... how can i get it instead of the inputstream? – Rafael Lima May 27 '18 at 00:29
  • 1
    It is not worked for localized raw folders. Any Idea?? – Rushabh Shah Jul 30 '21 at 10:54
50

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();
    }
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • 1
    The problem is that I can't use R.raw.XYZ because the name of XYZ is only available as a string. But the link of simon help me solve the problem. I will give a correct solution as a separate answer. – Bernd Apr 10 '13 at 19:14
  • 3
    this works perfectly fine `InputStream input = Context.getResources().openRawResource(R.raw.configjson);` – Someone Somewhere Dec 12 '13 at 18:40
14

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.

Avi
  • 21,182
  • 26
  • 82
  • 121
user2574545
  • 141
  • 1
  • 2
4

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)
Morgan Koh
  • 2,297
  • 24
  • 24