Query picture
First, query the Android native database with the URI of the image Images.Media.EXTERNAL_CONTENT_URI .
The following is a specific query instance FileInfo for the custom data model.
PublicArraylist<fileinfo>Queryallimage(FinalContext context) {if(Context = =NULL) {//Determine the validity of incoming parameters return NULL; } arraylist<fileinfo> images =NewArraylist<fileinfo> (); Contentresolver resolver = Context.getcontentresolver (); cursor cursor =NULL;Try{//Query database with parameters (path, column name to query, conditional statement, condition parameter, sort)cursor = Resolver.query (Images.Media.EXTERNAL_CONTENT_URI,NULL,NULL,NULL,NULL);if(Cursor! =NULL) { while(Cursor.movetonext ()) {FileInfo image =NewFileInfo (); Image.setid (Cursor.getint (Cursor.getcolumnindex (images.media._id)));//Get unique IDImage.setfilepath (Cursor.getstring (Cursor.getcolumnindex (Images.Media.DATA)));//file pathImage.setfilename (Cursor.getstring (Cursor.getcolumnindex (Images.Media.DISPLAY_NAME)));//File name //... There are many properties that you can set //You can view the property name from the next line, and then search for the corresponding constant name in Images.media.LOG.I (TAG,"Queryallimage---All column name---"+ Cursor.getcolumnname (cursor.getposition ()));//Get thumbnails (if the volume of data is large, it will be time-consuming-think about how to create a strand preempted) / * * can access android.provider.MediaStore.Images.Thumbnails query picture thumbnail * Thumbnails under the Getthumbna The Il method can obtain a picture thumbnail, where the third parameter type can also be selected Mini_kind * /Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail (resolver, Image.getid (), Images.Thumbnails.MICRO_KIND,NULL); Image.setthumbnail (thumbnail); Images.add (image); } } }Catch(Exception e) {E.printstacktrace (); }finally{if(Cursor! =NULL) {cursor.close (); } }returnImages;}
Query Audio
Also first query the database to obtain audio-related information, the audio URI is Audio.Media.EXTERNAL_CONTENT_URI
The following is a specific query instance FileInfo for the custom data model.
PublicArraylist<fileinfo>Queryallaudio(FinalContext context) {if(Context = =NULL) {//Determine the validity of incoming parameters return NULL; } arraylist<fileinfo> audios =NewArraylist<fileinfo> (); Contentresolver resolver = Context.getcontentresolver (); cursor cursor =NULL;Try{//Query database with parameters (path, column name to query, conditional statement, condition parameter, sort)cursor = Resolver.query (Audio.Media.EXTERNAL_CONTENT_URI,NULL,NULL,NULL,NULL);if(Cursor! =NULL) { while(Cursor.movetonext ()) {FileInfo audio =NewFileInfo (); Audio.setid (Cursor.getint (Cursor.getcolumnindex (audio.media._id)));//Get unique IDAudio.setfilepath (Cursor.getstring (Cursor.getcolumnindex (Audio.Media.DATA)));//file pathAudio.setfilename (Cursor.getstring (Cursor.getcolumnindex (Audio.Media.DISPLAY_NAME)));//File name //... There are many properties that you can set //You can view the property name from the next line, then go to Audio.media to find the corresponding constant nameLOG.I (TAG,"Queryallimage---All column name---"+ Cursor.getcolumnname (cursor.getposition ()));//Get album art (it can be time consuming if the data is large--you need to think about how to create a strand preempted)Bitmap Albumart = Creatalbumart (Audio.getfilepath ()); Audio.setthumbnail (Albumart); Audios.add (audio); } } }Catch(Exception e) {E.printstacktrace (); }finally{if(Cursor! =NULL) {cursor.close (); } }returnAudios;}/** * @Description Get album art * @param filePath file path, like Xxx/xxx/xx.mp3 * @return album cover bitmap * / PublicBitmapCreatealbumart(FinalString filePath) {Bitmap Bitmap =NULL;//A class capable of obtaining metadata for multimedia filesMediametadataretriever Retriever =NewMediametadataretriever ();Try{Retriever.setdatasource (FilePath);//Set data source byte[] Embedpic = Retriever.getembeddedpicture ();//Get byte-type dataBitmap = Bitmapfactory.decodebytearray (art,0, art.length);//Convert to Picture}Catch(Exception e) {E.printstacktrace (); }finally{Try{retriever.release (); }Catch(Exception E2) {E2.printstacktrace (); } }returnBitmap;}
Query video
The method of querying the video thumbnail is very similar to the picture.
The following is a specific query instance FileInfo for the custom data model.
PublicArraylist<fileinfo>Queryallvideo(FinalContext context) {if(Context = =NULL) {//Determine the validity of incoming parameters return NULL; } arraylist<fileinfo> Videos =NewArraylist<fileinfo> (); Contentresolver resolver = Context.getcontentresolver (); cursor cursor =NULL;Try{//Query database with parameters (path, column name to query, conditional statement, condition parameter, sort)cursor = Resolver.query (Video.Media.EXTERNAL_CONTENT_URI,NULL,NULL,NULL,NULL);if(Cursor! =NULL) { while(Cursor.movetonext ()) {FileInfo video =NewFileInfo (); Video.setid (Cursor.getint (Cursor.getcolumnindex (video.media._id)));//Get unique IDVideo.setfilepath (Cursor.getstring (Cursor.getcolumnindex (Video.Media.DATA)));//file pathVideo.setfilename (Cursor.getstring (Cursor.getcolumnindex (Video.Media.DISPLAY_NAME)));//File name //... There are many properties that you can set //You can view the property name from the next line, and then search for the corresponding constant name in Video.media.LOG.I (TAG,"Queryallimage---All column name---"+ Cursor.getcolumnname (cursor.getposition ()));//Get thumbnails (if the volume of data is large, it will be time-consuming-think about how to create a strand preempted) / * * can access android.provider.MediaStore.Video.Thumbnails query picture thumbnail * Thumbnails under the Getthumbnai The L method can obtain a picture thumbnail, where the third parameter type can also be selected Mini_kind * /Bitmap thumbnail = MediaStore.Video.Thumbnails.getThumbnail (resolver, Image.getid (), Video.Thumbnails.MICRO_KIND,NULL); Video.setthumbnail (thumbnail); Videos.add (image); } } }Catch(Exception e) {E.printstacktrace (); }finally{if(Cursor! =NULL) {cursor.close (); } }returnVideos;}
The above method is effective by personal practice, if you have any questions, please leave a message to tell me.
Android gets all pictures, video thumbnails and music album covers on SD card