59

In J2ME, I've do this like that: getClass().getResourceAsStream("/raw_resources.dat");

But in android, I always get null on this, why?

2
  • 20
    There is a huge difference between programming for android and J2ME
    – Janusz
    May 18, 2010 at 11:05
  • 3
    It's funny to read such a comment on a question about something that works identically on Android and J2ME. See my answer.
    – 0xF
    Feb 4, 2014 at 22:17

8 Answers 8

134

For raw files, you should consider creating a raw folder inside res directory and then call getResources().openRawResource(resourceName) from your activity.

8
  • Samuh, thanks for your reply, if i do this, i've got errors Android XML Format Problem, because i use not standard xml, is there other way to use it as really raw data?
    – Arkaha
    May 18, 2010 at 16:03
  • what do you mean by non-standard XML? You can bundle XML files in assets also.
    – Samuh
    May 18, 2010 at 16:26
  • Samuh, i'm porting j2me application, and it has files with extension *.xml, content of such files very similar to xml, but not the same, eclipse shouting on this files, because it can't compile it. probably i can set some kind of "ignore" on this files? but i don't know how..
    – Arkaha
    May 18, 2010 at 18:50
  • Files in res/raw are not processed in any way by the resource compiler, so will not generate errors because of their content.
    – hackbod
    May 19, 2010 at 1:30
  • 1
    @Guy: common problem when you have different R's. You are simply not referencing the right R. Check your imports or reference the right R that has the resource.
    – m0skit0
    Nov 16, 2012 at 11:25
25
InputStream raw = context.getAssets().open("filename.ext");

Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
7
  • 20
    Please see the answer by Samuh (below), it is more accurate.
    – BoD
    Oct 13, 2011 at 12:14
  • 1
    As a side note, raw folder does not allow access by filename. Some apps need dynamic filenames so in such cases assets folder is useful
    – Caner
    Nov 29, 2011 at 11:10
  • 9
    he didn't ask about assets/ he asked about res/raw/ Feb 16, 2012 at 17:54
  • 2
    This is not the way to do it in Android. Check Samuh answer.
    – m0skit0
    Nov 16, 2012 at 11:24
  • 2
    He said res folder, not assets folder. Feb 13, 2013 at 23:54
16

In some situations we have to get image from drawable or raw folder using image name instead if generated id

// Image View Object 
        mIv = (ImageView) findViewById(R.id.xidIma);
// create context Object for  to Fetch  image from resourse 
Context mContext=getApplicationContext();

// getResources().getIdentifier("image_name","res_folder_name", package_name);

// find out below example 
    int i = mContext.getResources().getIdentifier("ic_launcher","raw", mContext.getPackageName());

// now we will get contsant id for that image       
        mIv.setBackgroundResource(i);
1
  • 6
    This line: int i = mContext.getResources().getIdentifier("ic_launcher","raw", mContext.getPackageName()); is GOLD
    – Radu
    Apr 27, 2014 at 11:56
9

Android access to raw resources

An advance approach is using Kotlin Extension function

fun Context.getRawInput(@RawRes resourceId: Int): InputStream {
    return resources.openRawResource(resourceId)
}

One more interesting thing is extension function use that is defined in Closeable scope

For example you can work with input stream in elegant way without handling Exceptions and memory managing

fun Context.readRaw(@RawRes resourceId: Int): String {
    return resources.openRawResource(resourceId).bufferedReader(Charsets.UTF_8).use { it.readText() }
}
6
TextView txtvw = (TextView)findViewById(R.id.TextView01);
        txtvw.setText(readTxt());

 private String readTxt()
    {
    InputStream raw = getResources().openRawResource(R.raw.hello);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;
    try
    {
        i = raw.read();
        while (i != -1)
        {
            byteArrayOutputStream.write(i);
            i = raw.read();
        }
        raw.close();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block

        e.printStackTrace();
    }


    return byteArrayOutputStream.toString();

}

TextView01:: txtview in linearlayout hello:: .txt file in res/raw folder (u can access ny othr folder as wel)

Ist 2 lines are 2 written in onCreate() method

rest is to be written in class extending Activity!!

5

getClass().getResourcesAsStream() works fine on Android. Just make sure the file you are trying to open is correctly embedded in your APK (open the APK as ZIP).

Normally on Android you put such files in the assets directory. So if you put the raw_resources.dat in the assets subdirectory of your project, it will end up in the assets directory in the APK and you can use:

getClass().getResourcesAsStream("/assets/raw_resources.dat");

It is also possible to customize the build process so that the file doesn't land in the assets directory in the APK.

2
  • 2
    Eyy, nice. Doesn't even need a Context.
    – Erhannis
    Mar 24, 2020 at 17:51
  • 1
    It should be noted that the assets folder is located as src/main/assets.
    – Erhannis
    Mar 24, 2020 at 17:51
3

InputStream in = getResources().openRawResource(resourceName);

This will work correctly. Before that you have to create the xml file / text file in raw resource. Then it will be accessible.

Edit
Some times com.andriod.R will be imported if there is any error in layout file or image names. So You have to import package correctly, then only the raw file will be accessible.

4
-1

This worked for for me: getResources().openRawResource(R.raw.certificate)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.