-2

I'm developing a test class that have to check if there are all the resources. It's a specification mandatory that i access to the resources by file name (png's).

I know there's the possibility of loading as a resource, but I cannot load resources. I'm developing a test app. How I could access the file file.png in res/drawable named ?

new FileOutputStream("/res/drawable/xxx.png");
Joterito
  • 153
  • 2
  • 15

1 Answers1

0

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.

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
  • Thanks. I knwe how to access to resources as resources. But i need to access via path and file name. I have to do it in this way, no other. – Joterito Nov 23 '12 at 18:54
  • @Joterito what do you want to do? – hasanghaforian Nov 23 '12 at 18:55
  • First of all, sorry for my english, it's very poor. What i wan't to do is access to a png i have in drawable folder but not as resource. I want to access by the file name. new FileOutputStream("/res/drawable/xxx.png"); What i don't know is the path of the file in resources folder. /res/drawable/? (getFilesDir()+"/res/drawable? Sorry if i don't explain very well and thanks – Joterito Nov 23 '12 at 19:21