Zhao Yazhi _android's getresources () resource reference

Source: Internet
Author: User

Today to do an Android scratch music project, which used a lot of places to use the getresources.

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

in the beginning, 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 file to your project in the res/drawable/directory, from here, you can reference it to your code or your XML layout, that is, reference it can also use the resource number, such as you choose a file as long as the suffix is removed (for example: My_ Image.png refers to it is just 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 (); that's it.


Note that when using getresource (), note

1, must have a context

2, can be used as a member variable, construct incoming or method parameters passed in. You can do it.


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

After you get the resource using Getresources in your app, open the file with the Openrawresource method (the resource file name without the suffix). For example:

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 is named kb.html and does not require a suffix. html)  InputStreamReader read = new InputStreamReader (FS, "Gb2312″");  

read PNG or BMG under the Res/drawable directory
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);  


Related Article

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.