Today do an Android file manager, which used a lot of places to use the getresources.
drawable Currenticon = Null;currenticon = Getresources (). getdrawable (r.drawable.folder); CurrentIcon = GetResources (). Getdrawable (R.drawable.image);
At first, it was not quite understood why the Getresources () method could be used to obtain the resources of the existing system. So I looked at the document and read the information:
For example, if you put the resource file under the/raw/raw of the application, you can open the file with the Openrawresource method (the name of the resource file without the suffix) after you have used getresources to get the resource in your app. Like what:
Resources myresources = Getresources (); InputStream myFile = Myresources.openrawresource (r.raw.myfilename);
As with traditional Java file operations, the Openfileinput and Openfileoutput methods are available in the Android API to read files on the device.
Shorthand
InputStream FS =this.getresources (). Openrawresource (R.RAW.KB); (The resource file name is called kb.html and does not require a suffix. html) InputStreamReader read = new InputStreamReader (FS, "Gb2312″"); BufferedReader in = new BufferedReader (read);
Read PNG or BMG under the Res/drawable folder
Resources R = This.getcontext (). getresources ();//Read the resource in a data stream inputstream is = R.openrawresource ( R.drawable.my_background_image); Bitmapdrawable Bmpdraw = new Bitmapdrawable (IS); Bitmap bmp = Bmpdraw.getbitmap ();
Or
InputStream is = Getresources (). Openrawresource (R.drawable.icon); Bitmap Mbitmap = Bitmapfactory.decodestream (IS); Paint Mpaint = new paint (); Canvas.drawbitmap (Mbitmap, Max., mpaint);
Packet Package:android.content.res
Main categories: Resources
InputStream openrawresource (int id) gets the data stream of the resource, reads the resource data
Put a picture resource, add your file to your project in the res/drawable/folder, and from here, you can reference it to your code or your XML layout, that is, referencing it can also be used with the resource number, For example, if you choose a file, you can simply remove the suffix (e.g. my_image.png refers to it is my_image).
When you need to use the XML resources, you can use Context.getresources (). getdrawable (R .... The address of the resource such as: R.string.ok);
When you do not have a context parameter inside your method, you can This.getcontext (). Getresources ();
Note that when using getresource (), note
1, must have the context Ah
2, can be used as a member variable, constructs an incoming or method parameter incoming. will be able to.