Generally when writing Android programs, the picture resources are usually placed in the/res/drawable/folder, read, The image content can be read through R.drawable.imageid, but when the user is in use, it is common to want to read the resources stored on the memory card, when the above method will not work, it is necessary to use the bitmap and Bitmapfactory objects to load the resources on the phone disk.
First put a imageview in the layout file, the user placed the picture, the picture is stored in the path of/data/data/demo.jpg, in the program first get ImageView, the code is as follows:
ImageView myimage = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);
Then check to see if the file exists with the following code:
String fileName = "/data/data/demo.jpg";
File File = new file (fileName);
if (file.exists ()) {
Bitmap BM = bitmapfactory.decodefile (fileName);
Myimage.setimagebitmap (BM);
}else{
Textview.settext ("File does not exist");
}
Bitmapfactory can convert a picture to a bitmap object, and the DecodeFile () method can convert the picture file in the phone system to a bitmap object, while providing other ways to convert files in different locations, such as Decoderesource () can convert pre-deposited images in/res/drawable/into bitmap objects, Decodestream () method can be used to convert InputStream objects into bitmap objects.
Android loads the resources on the phone disk---the use of the DecodeFile method