Android getresources role and need to be aware of the point

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.