Android Get album list implementation (i)

Source: Internet
Author: User

The project implements the following functions :
Get Mobile album, click on each album to enter the picture list of the album interface, in the Picture list interface can achieve multiple selection of pictures, and then into the selected image interface, in this interface can achieve the selected image upload and other functions.
the biggest feature of the project:
1, to obtain a list of albums, the current network above the introduction of the album to obtain a few items, this article specifically about the acquisition of albums.
2, using the Android-universal-image-loader Integration Framework-third-party jar load local images, familiar with the jar developer is not unfamiliar, the jar is very powerful, in addition to access to network images, Local images are also possible . At the same time, an Oom exception can be effectively resolved by referencing a third-party jar.
Recently csdn may have problems, pictures can not be uploaded, so there is no way for everyone to see.
First of all, the principle of acquiring Mobile album:
We all know to get the phone pictures to use the Contentresolver this class, but through this class to get the album, is not available, can only get to the album ID, album cover pictures can not be obtained (at least I am writing a blog post, I do not know how to directly get the phone album, If you know how to directly obtain the album, do not blame Kazakhstan, I level limited ^_^, at the same time also please enlighten, please message Kazakhstan "handshake"). So the project in the realization of the acquisition of mobile phone album, is to get the ID of the phone album, and then get all the pictures of the mobile phone content, by comparing the photo album ID, each picture merged into the corresponding album.
Now give the class to get the album and the Inside picture of the album, get the album and picture in this class complete. The code is as follows:

/*** Asynchronous Threading class implements this function * /Public class photoupalbumhelper extends asynctask<object, object , Object>{    FinalString TAG = GetClass (). Getsimplename ();    Context context; Contentresolver CR;//Thumbnail listhashmap<string, string> thumbnaillist =NewHashmap<string, string> ();//Album ListlistNewArraylistNewHashmap<string, photoupimagebucket> ();PrivateGetalbumlist getalbumlist;//Get Instancespublic static Photoupalbumhelper Gethelper () {Photoupalbumhelper instance =NewPhotoupalbumhelper ();returnInstance }/** * Initialize * @param Context * *public void init (context context) {if( This. Context = =NULL) { This. Context = Context;        CR = Context.getcontentresolver (); }    }/** * Get thumbnails, here is the image of the main ID value * /    Privatevoid GetThumbnail () {string[] projection = {thumbnails._id, thumbnails.image_id, thumbnails.data}; Cursor Cursor1 = Thumbnails.queryminithumbnails (CR, Thumbnails.external_content_uri, Thumbnails.min        I_kind, projection);        Getthumbnailcolumndata (Cursor1);    Cursor1.close (); }/** * Get thumbnails from the database * @param cur */    Privatevoid Getthumbnailcolumndata (Cursor cur) {if(Cur.movetofirst ())            {int image_id;            String Image_path;            int image_idcolumn = Cur.getcolumnindex (thumbnails.image_id);            int dataColumn = Cur.getcolumnindex (Thumbnails.data);                do {image_id = Cur.getint (Image_idcolumn);                Image_path = cur.getstring (DataColumn); Thumbnaillist.put (""+ image_id, image_path); } while(Cur.movetonext ()); }    }/** * Did you create a picture set * /Boolean hasbuildimagesbucketlist =false;/** * Get Picture Collection * /    Privatevoid Buildimagesbucketlist () {//construct thumbnail indexGetThumbnail ();//Structure album indexString columns[] =NewString[] {media._id, media.bucket_id, media.picasa_id, Media.data, Media.display_name, Media.title, Media.size, media.bucket_display_name};//Get a cursorCursor cur = cr.query (media.external_content_uri, columns,NULL,NULL, media.date_modified+"desc");if(Cur.movetofirst ()) {//Gets the index of the specified columnint photoidindex = Cur.getcolumnindexorthrow (media._id);            int photopathindex = Cur.getcolumnindexorthrow (Media.data);            int bucketdisplaynameindex = Cur.getcolumnindexorthrow (media.bucket_display_name); int bucketidindex = Cur.getcolumnindexorthrow (media.bucket_id);/** * Description: This adds a judgment: determine if the name of the photo is legal, such as. jpg. png, etc. no name format * If the picture name is not valid, filter it directly out */do {if(Cur.getstring (Photopathindex). substring (cur.getstring (photopathindex). LastIndexOf ("/")+1, Cur.getstring (Photopathindex). LastIndexOf ("."). ReplaceAll (" ",""). Length () <=0) {LOG.D (TAG,"The address where the exception picture occurred: cur.getstring (photopathindex) ="+cur.getstring (Photopathindex)); }Else{String _id = cur.getstring (Photoidindex);                    String path = cur.getstring (Photopathindex);                    String bucketname = cur.getstring (Bucketdisplaynameindex);                    String bucketID = cur.getstring (Bucketidindex); Photoupimagebucket bucket = bucketlist.get (bucketID);//Here to complete the picture to merge into the response album                    if(Bucket = =NULL) {bucket =NewPhotoupimagebucket ();                        Bucketlist.put (bucketID, bucket); Bucket.imagelist =NewArraylist<photoupimageitem> ();                    Bucket.bucketname = Bucketname;                    } bucket.count++; Photoupimageitem Imageitem =NewPhotoupimageitem ();                    Imageitem.setimageid (_id);                    Imageitem.setimagepath (path);                Bucket.imageList.add (Imageitem); }            } while(Cur.movetonext ());        } cur.close (); Hasbuildimagesbucketlist =true; }/** * Get photo gallery * @param Refresh * @return  * *    PrivateList<photoupimagebucket> getimagesbucketlist (Boolean refresh) {if(Refresh | | (!refresh &&!hasbuildimagesbucketlist))        {buildimagesbucketlist (); } list<photoupimagebucket> tmplist =NewArraylist<photoupimagebucket> (); iterator<entry<string, photoupimagebucket>> ITR = Bucketlist.entryset (). Iterator ();//Convert hash to list         while(Itr.hasnext ())                    {map.entry<string, photoupimagebucket> Entry = (map.entry<string, photoupimagebucket>) ITR            . Next ();        Tmplist.add (Entry.getvalue ()); }returnTmplist; }/** * Get the original image path * @param image_id * @return * *    PrivateString Getoriginalimagepath (String image_id) {String path =NULL;        string[] projection = {media._id, media.data}; cursor cursor = cr.query (Media.external_content_uri, projection, media._id +"="+ image_id,NULL, media.date_modified+"desc");if(Cursor! =NULL) {Cursor.movetofirst ();        Path = cursor.getstring (Cursor.getcolumnindex (media.data)); }returnPath } public void Setgetalbumlist (Getalbumlist getalbumlist) { This. getalbumlist = getalbumlist; }//callback interface, when the completion of the album and picture acquisition, call the method of the interface to pass data, this method is very common, we must masterpublic interface getalbumlist{public void getalbumlist (list<photoupimagebucket> List); }@Override    protectedObject Doinbackground (Object ... params) {returnGetimagesbucketlist ((Boolean) (params[0])); }@SuppressWarnings("Unchecked")@Override    protectedvoid OnPostExecute (Object result) {Super. OnPostExecute (Result);    Getalbumlist.getalbumlist ((list<photoupimagebucket>) result); }}

The class uses an asynchronous threading class implementation and passes the data through the callback interface, passing the data through the following code after the asynchronous thread is executed.

protectedvoidonPostExecute(Object result) {        super.onPostExecute(result);        getAlbumList.getAlbumList((List<PhotoUpImageBucket>)result);    }

The data obtained above is obtained using the following methods in activity:

Private void LoadData() {photoupalbumhelper = Photoupalbumhelper.gethelper ();//Create an asynchronous thread instancePhotoupalbumhelper.init (albumsactivity. This);//Initialize instance        ///callback interface, create an anonymous internal object, implement the method in the interface, get the data passed to Photoupalbumhelper interface GetalbumlistPhotoupalbumhelper.setgetalbumlist (NewGetalbumlist () {@Override             Public void getalbumlist(list<photoupimagebucket> List)                {adapter.setarraylist (list); Adapter.notifydatasetchanged ();//Update ViewAlbumsactivity. This. list = list;        }        }); Photoupalbumhelper.execute (false);//Asynchronous Thread execution}

The following two posts describe the photo album interface and the image list interface in the selected album.

Android Get album list implementation (i)

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.