Android loads the resources on the phone disk---the use of the DecodeFile method

Source: Internet
Author: User

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

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.