Android advanced 2: Retrieving Android image libraries and displaying image details

Source: Internet
Author: User


In this article, we will learn how to retrieve and display images in the media library and the details of each image, including name, ID, path, size, and so on.

For more information about cursor, see blog: Concepts and usage of the cursor class in Android.

Specific implementation:


Package xiaosi. photolibrary; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. database. cursor; import android. graphics. bitmap; import android. graphics. drawable. drawable; import android. OS. bundle; import android. provider. mediastore. images. media; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. imageview; import android. widget. textview;/*** this class completes Image Retrieval. The display function is ** @ author administrator **/public class photolibraryactivity extends activity implements onclicklistener {private imageviewphoto; private buttonnext = NULL; private buttonprevious = NULL; private buttonmessage = NULL; private textviewposition = NULL; private cursorcursor; private intphotoindex; private intphotonameindex; private intphotoidindex; private intphototitleindex; private intphotosizeindex; private stringmessage = NULL; private inttotalnum = 0; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {next = (button) findviewbyid (R. id. next); next. setonclicklistener (this); previous = (button) findviewbyid (R. id. previous); previous. setonclicklistener (this); message = (button) findviewbyid (R. id. message); message. setonclicklistener (this); photo = (imageview) This. findviewbyid (R. id. image_view); position = (textview) findviewbyid (R. id. number); // specify the string columns [] = new string [] {media. data, media. _ id, media. title, media. display_name, media. size}; // get a cursor = This. getcontentresolver (). query (media. external_content_uri, columns, null); // obtain the index photoindex = cursor of the specified column. getcolumnindexorthrow (media. data); photonameindex = cursor. getcolumnindexorthrow (media. display_name); photoidindex = cursor. getcolumnindexorthrow (media. _ id); phototitleindex = cursor. getcolumnindexorthrow (media. title); photosizeindex = cursor. getcolumnindexorthrow (media. size); // get the total number of images totalnum = cursor. getcount (); // jump to the first image if (cursor. movetofirst () {setimage (); position. settext ("(1/" + totalnum + ")") ;}@ overridepublic void onclick (view arg0) {Switch (arg0.getid () {// next case R. id. next: If (cursor. movetonext () {setimage ();} else {cursor. movetolast ();} break; // previous case R. id. previous: If (cursor. movetoprevious () {setimage ();} else {cursor. movetofirst ();} break; case R. id. message: // dialog displays the details of alertdialog. builder = new alertdialog. builder (photolibraryactivity. this); builder. settitle ("details"); builder. setmessage (Message); builder. setpositivebutton ("off", new android. content. dialoginterface. onclicklistener () {public void onclick (dialoginterface diich, int which) {dialog. dismiss () ;}}); builder. show (); break ;}} private void setimage () {// obtain the namestring name of the image = cursor. getstring (photonameindex); // obtain the idstring Number of the image = cursor. getstring (photoidindex); // obtain the image's titlestring Title = cursor. getstring (phototitleindex); // obtain the image size. String size = cursor. getstring (photosizeindex); // obtain the image storage path string Path = cursor. getstring (photoindex); // assign a value for textview: Position (current position) position. settext ("(" + number + "/" + totalnum + ")"); message = "name:" + name + "\ n" + "Number: "+ number +" \ n "+" title: "+ title +" \ n "+" Size: "+ size +" \ n "+" Path: "+ path; // obtain the image drawable image = drawable through the path. createfrompath (PATH); photo. setimagedrawable (image );}}

Mian. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Background = "@ drawable/background" Android: orientation = "vertical"> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "50dip" Android: Orientation = "horizontal"> <button Android: id = "@ + ID/next" Android: layout_width = "60dip" Android: layout_height = "50dip" Android: Background = "@ drawable/next" Android: paddingleft = "30dip"/> <textview Android: Id = "@ + ID/number" Android: layout_width = "100dip" Android: layout_height = "50dip" Android: paddingleft = "30dip" Android: textcolor = "#000000"/> <button Android: Id = "@ + ID/previous" Android: layout_width = "60dip" Android: layout_height = "50dip" Android: Background = "@ drawable/previous" Android: paddingleft = "30dip"/> <button Android: Id = "@ + ID/message" Android: layout_width = "80dip" Android: layout_height = "40dip" Android: paddingleft = "30dip" Android: text = "details"/> </linearlayout> <imageview Android: id = "@ + ID/image_view" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"/> </linearlayout>



Let's witness the departure of cainiao ......

Let's talk about all the projects.






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.