SoFunction
Updated on 2025-03-02

Four ways to obtain image resources by Android programming

This article describes the exampleAndroidFour ways to obtain image resources by programming。 Share it for your reference, as follows:

1. Place the picture in the sdcard:

Copy the codeThe code is as follows:
Bitmap imageBitmap = (path)//path is the path of the image, and the directory is /sdcard

 
2. The picture is under the project's res folder
//Get the application objectApplicationInfo appInfo = getApplicationInfo();
//Get the id of the image (name is the name of the image, "drawable" is the directory where the image is stored, and it is the application package)int resID = getResources().getIdentifier(name, "drawable", );
//The code is as followspublic Bitmap getRes(String name) {
ApplicationInfo appInfo = getApplicationInfo();
int resID = getResources().getIdentifier(name, "drawable", );
return (getResources(), resID);
}

3. Place the picture in the src directory

String path = "com/xiangmu/"; //The path to store the pictureInputStream is = getClassLoader().getResourceAsStream(path); //Get picture stream

There is an Assets directory where you can store read-only files
 
The way to obtain resources is

Copy the codeThe code is as follows:
InputStream is = getResources().getAssets().open(name);

I hope this article will be helpful to everyone's Android programming design.