How to get access to raw resources that I put in res folder?

AndroidJava MeResourcesInputstreamAndroid Resources

Android Problem Overview


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

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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

InputStream raw = context.getAssets().open("filename.ext");

Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

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!!

Solution 6 - Android

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.

Solution 7 - Android

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

Solution 8 - Android

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

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
QuestionArkahaView Question on Stackoverflow
Solution 1 - AndroidSamuhView Answer on Stackoverflow
Solution 2 - AndroidAdrian VintuView Answer on Stackoverflow
Solution 3 - AndroidsravanView Answer on Stackoverflow
Solution 4 - AndroidyoAlex5View Answer on Stackoverflow
Solution 5 - Androidpoojan9118View Answer on Stackoverflow
Solution 6 - Android0xFView Answer on Stackoverflow
Solution 7 - Androidjava devView Answer on Stackoverflow
Solution 8 - AndroidIgniteCodersView Answer on Stackoverflow