Access resource files in Android
If you have a file in res/raw/textfile.txt
from your Activity/Widget call:
getResources().openRawResource(...)
returns an InputStream
The dots should actually be an integer found in R.raw... corresponding to your filename, possibly R.raw.textfile
(it's usually the name of the file without extension)
new BufferedInputStream(getResources().openRawResource(...));
then read the content of the file as a stream
Accessing a resource file in Android
Assuming you want to open a stream to process the file you should be doing this:
InputStream raw = getResources().openRawResource(R.raw.movie);
EDIT
Ok, now that I know exactly what you want, I have bad news. Not really possible.
See this SO question
Basically you would need to copy the file out of resources to somewhere that you can get at it like the SD card or internal storage.
How to load a resource file from url?
So if I'm understanding you, you need an input stream that reads your XML from a web server. That would look like this:
InputStream is = new URL("http://example.com/folder/configuration.xml").openConnection().getInputStream();
However there's much more to this, you can't do this on the main thread (see NetworkOnMainThreadException) and you have to be prepared that there is no internet connection or that it downloads slowly. So I don't know what you use this configuration for but for example you may have to have defaults and put a "please wait" message on the screen.
Access to resource folder route
It is recommended to read Resources Overview from Android docs,but if your problem is only about accessing resources,read Accessing Resources.So you can do what you want.
Summery,there is some ways to access resources that depends on situations,for example when you know name of resource or you know it's ID.And also it depends that where you want to access resources.
In your case I guess that you want to access a drawable
in your code,if it is true,you can do like this:
R.drawable.xxx
For example if name of your .png file is myimage and you have to set it in ImageView with name im do this:
ImageView im = ...;
im.setImageResource(R.drawable.myimage);
Edit:
If you have to get an InputStream
form a file in resources,you should consider creating a raw
folder inside res directory and put your file in it and then call getResources().openRawResource(resourceName) from your Activity or widget.For example :
InputStream ins = getResources().openRawResource(R.raw.test);
Here your file that is in raw folder is for example test.png
.
You can see more details here:
Using files as raw resources in Android
Android: Loading files from the Assets and Raw folders
stackoverflow
stackoverflow
stackoverflow
Finally if you want to create an output file from your resource,get an InputStream from it and write it's content to an OutputStream,you can see a good example here.
Android, accessing resource files from a native/NDK shared library
According to this answer, the NDK provides an API to access the assets folder (AAssetManager
, see here).
Alternatively, an easy way that I used in one of my projects is to write the necessary files to the filesystem (e.g. /storage or /sdcard) on the Java side and then only pass the filepath to the NDK.
How to get access to raw resources that I put in res folder?
InputStream raw = context.getAssets().open("filename.ext");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
Android access to resources outside of activity
If the folders are included in the Project Build Path, you can use ClassLoader access files under them outside the android.content.Context, for instance, from a POJO (in case if you don't want to pass a reference of android.content.Context):
String file = "res/raw/test.txt";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);
- The res folder is included into Project Build Path by default by all Android SDK version so far.
- The assets folder was included into Project Build Path by default before Android SDK r14.
To add folders into Project Build Path, right click your project -- Build Path -- Configure Build Path, add your folder (for example, assets if using later SDK version) as a Source folder in build path.
Check out the similar question I answered before at here.
Related Topics
How to Use Jsp/Jstl to Generate Dynamic CSS/JavaScript Files
Unobtrusive Way to Combine and Compress JavaScript/CSS for Java/Spring/Maven Applications
How to Clear the (Css) Visited History of an Android Webview
Selenium Webdriver - Getcssvalue() Method
Dynamically Add CSS Stylesheets in Javafx
How to Set -Fx-Max-Width to Use_Pref_Size in Javafx CSS
How to Get the Selected Index of a Radiogroup in Android
How to Set Background Color of an Activity to White Programmatically
How to Manage Cookies with Httpclient in Android And/Or Java
Getextractedtext on Inactive Inputconnection Warning on Android
Android Project Using Httpclient --> Http.Client (Apache), Post/Get Method
Rxjava Android How to Use the Zip Operator
Map.Clear() VS New Map:Which One Will Be Better
Registering a Headset Button Click with Broadcastreceiver in Android
How to Convert 24 Hr Format Time in to 12 Hr Format
Android Studio - No Jvm Installation Found