[Android] Resource path

Source: Internet
Author: User
Ideas:

Http://www.eoeandroid.com/thread-81618-1-1.html

If the image is under drawable, you can save the image ID to the database,
To save the path, you can put the image under the assets folder.

 

Absolute path:

Http://blog.csdn.net/svrsimon/article/details/7079320

Method 1:

String Path = file: // android_asset/file name;

Method 2:

Inputstream abpath = getclass (). getresourceasstream ("/assets/file name"); // to convert to string type string Path = new string (inputstreamtobyte (abpath); Private byte [] inputstreamtobyte (inputstream is) throws ioexception {bytearrayoutputstream bytestream = new bytearrayoutputstream (); int ch; while (CH = is. read ())! =-1) {bytestream. Write (CH);} byte imgdata [] = bytestream. tobytearray (); bytestream. Close (); Return imgdata ;}

 

Differences between the Assets Directory and the Res/raw and Res/drawable Directories

Http://superonion.iteye.com/blog/1424244

1. The resource files in the Assets Directory do not automatically generate IDS in R. java. Therefore, the file path must be specified to read files in the Assets Directory. You can access these files through the assetmanager class. To read background.png under the assetsdirectory:

Bitmap bgimg = getimagefromassetfile ("background.png");/*** read image from assets */private bitmap getimagefromassetsfile (string filename) {bitmap image = NULL; assetmanager AM = getresources (). getassets (); try {inputstream is = aM. open (filename); image = bitmapfactory. decodestream (is); is. close ();} catch (ioexception e) {e. printstacktrace ();} return image ;}

 

1. Place the image in sdcard,

Bitmap imagebitmap = bitmapfactory. decodefile (PATH) (path is the path of the image, and the directory is/sdcard)

2. The image is in the res folder of the project.

// Obtain the application object applicationinfo appinfo = getapplicationinfo (); // obtain the image ID (name is the image name, "drawable" is the directory where the image is stored, appinfo. packagename is the package of the application.) int resid = getresources (). getidentifier (name, "drawable", appinfo. packagename); // The Code is as follows: public bitmap getres (string name) {applicationinfo appinfo = getapplicationinfo (); int resid = getresources (). getidentifier (name, "drawable", appinfo. packagename); Return bitmapfactory. decoderesource (getresources (), resid );}

3. Place the image in the src directory.

String Path = "com/xiangmu/test.png"; // The path where the image is stored. inputstream is = getclassloader (). getresourceasstream (PATH); // obtain the image stream

4. There is an Assets Directory in Android. Read-Only files can be stored here.

 

// The method for obtaining resources is inputstream is = getresources (). getassets (). Open (name );
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.