How to Get Access to Raw Resources That I Put in Res Folder

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

Accessing res/raw folder in Android

Use InputStream to read file content.

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

To get it as a InputStream

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

OR

InputStream inputStream = getResources().openRawResource(R.raw.yourresource);

refer http://developer.android.com/guide/topics/resources/providing-resources.html for Providing Resources.

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.

How can I get file path on raw resources in Android Studio?


how can i get file path on raw resources?

You can't. There is no path. It is a file on your development machine. It is not a file on the device. Instead, it is an entry in the APK file that is your app on the device.

If your library supports an InputStream instead of a filesystem path, getResources().openRawResource() on a Context to get an InputStream on your raw resource.

Accessing contents of res/raw programmatically (Android)

You could use reflection.


ArrayList<Integer> list = new ArrayList<Integer>();
Field[] fields = R.raw.class.getFields();
for(Field f : fields)
try {
list.add(f.getInt(null));
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) { }

After that list holds all resource IDs in the res/raw folder.
If you need the name of the resource... f.getName() returns it.

How to read file from res/raw by name

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

Access resources folder and list of files in raw folder in package in Android

There are no files. Raw resources are resources, not files.

You are welcome to:

  • Have a "table of contents" resource that lists the other ones, as a <string-array> or an XML resource or something

  • Use reflection to iterate over the R.raw values

How to list folders in raw resources


However, I am having issues trying to access the folders I have in my application's res/raw/ folder.

You cannot have folders inside of res/raw/. A resource directory can contain files, not subdirectories.

If you want to package a directory tree with your app, use assets/ and AssetManager, not raw resources.

Full path to files in res/raw folder


My intention is to get the full path to this file

Resources are not files on the device. They are files on your development machine. They are entries in an APK file on the device.

Is there a way to get the absolute file path to files in res or assets folder?

No, sorry.

If its not possible in these two folders, how about external storage?

I would use internal storage, such as getFilesDir() or getCacheDir() on Context.



Related Topics



Leave a reply



Submit