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.