Zhao Yazhi _android's getresources () resource reference

Source: Internet
Author: User

Do an Android scratch-scratch project today. It used a lot of places to use the getresources.

<span style= "FONT-SIZE:12PX;" >//get Picture//Parameter 1:res is a reference to the resource, the 2:id is the idafter of the picture = Bitmapfactory.decoderesource (Getresources (), r.drawable.b); before = Bitmapfactory.decoderesource (Getresources (), R.drawable.a);</span>

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:

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 files to your project in the res/drawable/folder and from here you will be able to reference it to your code or your XML layout, that is to say. Referencing it can also be used as a 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 XML resources. will be able to 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

2, can be used as a member variable, constructs an incoming or method parameter incoming. will be able to.


Put the resource file under the/raw/raw of the application

After you use Getresources in your app to get resources. Open the file with the Openrawresource method (the name of the resource file without the suffix). 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.

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

read PNG or BMG under the Res/drawable folder
Get Resources Object  r = This.getcontext (). Getresources ();  Reads a resource as a data stream  InputStream is = R.openrawresource (r.drawable.my_background_image);  Bitmapdrawable Bmpdraw = new Bitmapdrawable (is);  Bitmap bmp = Bmpdraw.getbitmap ();  
Method 2:
InputStream is = Getresources (). Openrawresource (R.drawable.icon);  Bitmap Mbitmap = Bitmapfactory.decodestream (is);  Paint Mpaint = new paint ();  Canvas.drawbitmap (Mbitmap, Max, mpaint);  


Zhao Yazhi _android's getresources () resource reference

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.