Android obtains all pictures, video thumbnails, and album covers on the SD card.

Source: Internet
Author: User

Android obtains all pictures, video thumbnails, and album covers on the SD card.
Query Images

First, query the inherent Android database. The image Uri isImages.Media.EXTERNAL_CONTENT_URI.
The following is a specific instance query,FileInfoIs a custom data model.

Public ArrayList
  
   
QueryAllImage (final Context context) {if (context = null) {// judge the validity of the input parameter return null;} ArrayList
   
    
Images = new ArrayList
    
     
(); ContentResolver resolver = context. getContentResolver (); Cursor cursor = null; try {// query the database. The parameters are (paths, column names to be queried, condition statements, condition parameters, and sorting) cursor = resolver. query (Images. media. EXTERNAL_CONTENT_URI, null); if (cursor! = Null) {while (cursor. moveToNext () {FileInfo image = new FileInfo (); image. setId (cursor. getInt (cursor. getColumnIndex (Images. media. _ ID); // obtain the unique id image. setFilePath (cursor. getString (cursor. getColumnIndex (Images. media. DATA); // file path image. setFileName (cursor. getString (cursor. getColumnIndex (Images. media. DISPLAY_NAME); // file name //... there are also many attributes that can be set // you can view the attribute name in the next line, and then in Images. media. find the corresponding constant name Log. I (TAG, "qu EryAllImage --- all column name --- "+ cursor. getColumnName (cursor. getPosition (); // obtain the thumbnail (if the data volume is large, it will take a lot of time-you need to consider how to enable sub-thread loading)/** you can access android. provider. mediaStore. images. thumbnails query image Thumbnails * The getThumbnail method under Thumbnails can obtain image Thumbnails. The third parameter type can also be MINI_KIND */Bitmap thumbnail = MediaStore. images. thumbnails. getThumbnail (resolver, image. getId (), Images. thumbnails. MICRO_KIND, null); image. setThumbnail (thumbnai L); images. add (image) ;}} catch (Exception e) {e. printStackTrace () ;}finally {if (cursor! = Null) {cursor. close () ;}return images ;}
    
   
  
Query audio

First query the database to obtain Audio Information. The audio Uri isAudio.Media.EXTERNAL_CONTENT_URI
The following is a specific instance query,FileInfoIs a custom data model.

Public ArrayList
  
   
QueryAllAudio (final Context context) {if (context = null) {// judge the validity of the input parameter return null;} ArrayList
   
    
Audios = new ArrayList
    
     
(); ContentResolver resolver = context. getContentResolver (); Cursor cursor = null; try {// query the database. The parameters are (paths, column names to be queried, condition statements, condition parameters, and sorting) cursor = resolver. query (Audio. media. EXTERNAL_CONTENT_URI, null); if (cursor! = Null) {while (cursor. moveToNext () {FileInfo audio = new FileInfo (); audio. setId (cursor. getInt (cursor. getColumnIndex (Audio. media. _ ID); // obtain the unique id audio. setFilePath (cursor. getString (cursor. getColumnIndex (Audio. media. DATA); // file path audio. setFileName (cursor. getString (cursor. getColumnIndex (Audio. media. DISPLAY_NAME); // file name //... there are also many attributes that can be set // you can view the attribute name in the next line, and then go to Audio. find the corresponding constant Name Log in Media. I (TAG, "queryAl LImage --- all column name --- "+ cursor. getColumnName (cursor. getPosition (); // obtain the album cover (if the data volume is large, it will take a lot of time-you need to consider how to enable the subthread to load) Bitmap albumArt = creatAlbumArt (audio. getFilePath (); audio. setThumbnail (albumArt); audios. add (audio) ;}} catch (Exception e) {e. printStackTrace ();} finally {if (cursor! = Null) {cursor. close () ;}return audios;}/*** @ Description: Obtain the album art * @ param filePath, like XXX/XX.mp3 * @ return album art bitmap */public Bitmap createAlbumArt (final String filePath) {Bitmap bitmap = null; // MediaMetadataRetriever retriever = new MediaMetadataRetriever (); try {retriever. setDataSource (filePath); // set the Data Source byte [] embedPic = retriever. getEmbeddedPicture (); // obtain the byte data bitmap = BitmapFactory. decodeByteArray (art, 0, art. length); // convert to image} catch (Exception e) {e. printStackTrace ();} finally {try {retriever. release () ;}catch (Exception e2) {e2.printStackTrace () ;}} return bitmap ;}
    
   
  
Query video

The method for querying a video thumbnail is similar to that for an image.
The following is a specific instance query,FileInfoIs a custom data model.

Public ArrayList
  
   
QueryAllVideo (final Context context) {if (context = null) {// judge the validity of the input parameter return null;} ArrayList
   
    
Videos = new ArrayList
    
     
(); ContentResolver resolver = context. getContentResolver (); Cursor cursor = null; try {// query the database. The parameters are (paths, column names to be queried, condition statements, condition parameters, and sorting) cursor = resolver. query (Video. media. EXTERNAL_CONTENT_URI, null); if (cursor! = Null) {while (cursor. moveToNext () {FileInfo video = new FileInfo (); video. setId (cursor. getInt (cursor. getColumnIndex (Video. media. _ ID); // obtain the unique id video. setFilePath (cursor. getString (cursor. getColumnIndex (Video. media. DATA); // file path video. setFileName (cursor. getString (cursor. getColumnIndex (Video. media. DISPLAY_NAME); // file name //... there are also many attributes that can be set // you can view the attribute name in the next line, and then in Video. media. find the corresponding constant name Log. I (TAG, "queryA LlImage --- all column name --- "+ cursor. getColumnName (cursor. getPosition (); // obtain the thumbnail (if the data volume is large, it will take a lot of time-you need to consider how to enable sub-thread loading)/** you can access android. provider. mediaStore. video. thumbnails query image Thumbnails * The getThumbnail method under Thumbnails can obtain image Thumbnails. The third parameter type can also be MINI_KIND */Bitmap thumbnail = MediaStore. video. thumbnails. getThumbnail (resolver, image. getId (), Video. thumbnails. MICRO_KIND, null); video. setThumbnail (thumbnail); vid Eos. add (image) ;}} catch (Exception e) {e. printStackTrace () ;}finally {if (cursor! = Null) {cursor. close () ;}return videos ;}
    
   
  

The above methods have been put into practice. If you have any questions, please leave a message to me.

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.