4 ways to get picture resources on Android

Source: Internet
Author: User

1. Picture in SDcard

Bitmap imagebitmap = bitmapfactory.decodefile (path) (paths are images, and directories are/sdcard)

2. Picture below the Res folder of the project

Get Application Object

ApplicationInfo appInfo = Getapplicationinfo ();

Get the image ID (name is the name of the picture, "Drawable" is the directory where the picture is stored, Appinfo.packagename is the app's package)

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. The image is placed in the SRC directory

String Path = "Com/xiangmu/test.png"; Path of picture storage

InputStream is = getClassLoader (). getResourceAsStream (path); Get picture Stream

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

The way resources are obtained is

InputStream is = Getresources (). Getassets (). open (name);

4 ways to get picture resources on Android

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.