Android---Read system resource function getresources () Summary

Source: Internet
Author: User

Turn http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1201/655.html

I. Basic INTRODUCTION

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, you can reference it in the code or XML layout, you can also use the resource number, such as you choose a file as long as the suffix is removed (for example: Mm_image.png refers to it is mm_ 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.
Here is a detailed description of the usage scenario:

1, need to use GetResource () must pay attention to
Must have a context, this General Service or activity is
Can be used as a member variable to construct an incoming or method parameter pass in.


2. When referencing an XML file, you may pass:
Getresources (). GETXML () Gets the XML original file and then obtains the Xmlresourceparser object
Xmlresourceparser XRP = Mres.getxml (r.xml.personal);

The use of R .... You can specify an XML file under the folder to load using

3, some other file read method
A, put the resource files under the/raw/raw of the application, then you can use Getresources in the application to get the resources,

Open this file with the Openrawresource method (resource file name without suffix)

Resources myresources = Getresources ();    InputStream myFile = Myresources.openrawresource (r.raw.xx_filename);

Like normal Java programs, Android provides the Openfileinput and Openfileoutput methods to read files on the device

InputStream fs =this. Getresources (). Openrawresource (r.raw.index.htm); (The resource file is named index.html and does not require a suffix.    htm) New InputStreamReader (FS, "Utf-8");     New BufferedReader (read);

b, read PNG or BMP in the res/drawable directory

// get the Resources    object  This . GetContext (). Getresources ();     // read a resource    in a data flow way InputStream is = R.openrawresource (r.drawable.mm_image);     New bitmapdrawable (IS);     = Bmpdraw.getbitmap ();

If you need to take advantage of the picture decoder, use as follows:

InputStream is = getresources (). Openrawresource (R.drawable.icon);     = Bitmapfactory.decodestream (is);     New Paint ();     Mpaint);

Note When using GetResource ()

1, must have the context Ah 2, can be used as a member variable, construct incoming or method parameters passed in. You can do it.

Android---Read system resource function getresources () Summary

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.