Android multimedia learning 2: Retrieving and displaying Android image Libraries

Source: Internet
Author: User

In the previous article, the examples show how to call camera in Android and store images. In this article, we will learn how to retrieve and display images in the library.

 

Note that if you have pushed several images to the sdcard when the simulator has been started, we recommend that you disable the simulator and restart it.

Otherwise, the image you just added cannot be obtained. This is because Android scans multimedia files in sdcard on the simulator when the system starts.

 

 

Let's talk less about the Code:

 

Package demo. camera; <br/> Import android. app. activity; <br/> Import android. database. cursor; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. OS. bundle; <br/> Import android. provider. mediastore. images. media; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. widget. imagebutton; <br/> Import android. widget. textview; <br/> /** <Br/> * this class completes image retrieval, display function <br/> * @ author administrator <br/> */<br/> public class photomanager extends activity {</P> <p> Public static final float display_width = 200; <br/> Public static final float display_height = 200; </P> <p> // The reason why imagebutton is used as a button <br/> private imagebutton photoview; <br/> private textview nameview; </P> <p> private cursor; </P> <p> private string photopath; // save Put the location information corresponding to an image <br/> private bitmap currphoto; </P> <p> // these three variables are mainly used to save media. data, media. title, media. display_name index number to obtain data in each column <br/> private int photoindex; <br/> // Private int titleindex; <br/> private int nameindex; </P> <p> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. photo_view); </P> <p> photoview = (imagebutton) This. findviewbyid (R. id. im Age_view); <br/> photoview. setonclicklistener (clicklistener); <br/> nameview = (textview) This. findviewbyid (R. id. view_name); </P> <p> // specify the column to be obtained <br/> string columns [] = new string [] {<br/> media. data, media. _ id, media. title, media. display_name <br/>}; <br/> // cursor = This. managedquery (media. external_content_uri, columns, null); <br/> cursor = This. getcontentresolver (). query (media. external_content_u Ri, columns, null); <br/> photoindex = cursor. getcolumnindexorthrow (media. data); <br/> // titleindex = cursor. getcolumnindexorthrow (media. title); <br/> nameindex = cursor. getcolumnindexorthrow (media. display_name); </P> <p> log. V ("here first:", "first debug"); <br/> // display the first image, but you must first judge it, whether cursor has a value <br/> If (cursor. movetofirst () {<br/> showimage (); <br/>}</P> <p> private view. onclicklistener C Licklistener = new view. onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {</P> <p> If (cursor. movetonext () {<br/> showimage (); <br/>}< br/> }; </P> <p>/** <br/> * display image information <br/> */<br/> private void showimage () {<br/> photopath = cursor. getstring (photoindex); // The location information of the image storage obtained here <br/> // how to obtain the image here? View decodebitmap <br/> log. V ("Photo path:", photopath); <br/> currphoto = decodebitmap (photopath); <br/> photoview. setimagebitmap (currphoto); <br/> nameview. settext (cursor. getstring (nameindex )); <br/>}</P> <p>/** <br/> * obtain image information from path <br/> * @ Param path <br/> *@ return <br/> */<br/> private bitmap decodebitmap (string path) {<br/> bitmapfactory. options op = new bitmapfactory. options (); <br/> op. injustdecodebounds = true; <br/> bitmap BMP = bitmapfactory. decodefile (path, OP); // obtain the size information <br/> // obtain the proportional size <br/> int wratio = (INT) math. ceil (op. outwidth/display_width); <br/> int hratio = (INT) math. ceil (op. outheight/display_height); <br/> // if the specified size is exceeded, the ratio is reduced. <br/> If (wratio> 1 & hratio> 1) {<br/> If (wratio> hratio) {<br/> op. insamplesize = wratio; <br/>} else {<br/> op. insamplesize = hratio; <br/>}< br/> op. injustdecodebounds = false; <br/> BMP = bitmapfactory. decodefile (path, OP); <br/> return BMP; <br/>}</P> <p >}< br/>

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.