Four ways to get picture resources from Android programming _android

Source: Internet
Author: User

This article illustrates four ways to get picture resources from the Android programming implementation. Share to everyone for your reference, specific as follows:

1. The picture is placed in the SDcard:

Copy Code code as follows:
Bitmap imagebitmap = bitmapfactory.decodefile (path)//path is the path to the picture, and the directory is/sdcard


2. The picture is under the project's Res folder

Get Application Object
applicationinfo appInfo = Getapplicationinfo ();
To get the ID of the picture (name is the picture, "Drawable" is the directory in which the picture resides, Appinfo.packagename is the package of the application)
int resid = Getresources (). Getidentifier (name, "drawable", appinfo.packagename);
The code follows public
Bitmap Getres (String name) {
ApplicationInfo appInfo = Getapplicationinfo ();
int resid = Getresources (). Getidentifier (Name, "drawable", appinfo.packagename);
Return Bitmapfactory.decoderesource (Getresources (), resid);
}

3. The picture is placed in the SRC directory

String Path = "Com/xiangmu/test.png"; The picture holds the path
inputstream is = getClassLoader (). getResourceAsStream (path);//Get Picture stream

There is a assets directory in 4.android, where you can store read-only files

Resources are obtained in the form of

Copy Code code as follows:
InputStream is = Getresources (). Getassets (). open (name);

I hope this article will help you with the Android program.

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.