Android MediaStore retrieves and plays a video

Source: Internet
Author: User

This article is intended to retrieve the video in the SD card on the mobile phone, and then display the thumbnails, names, and other video information of the retrieved video on the ListView. Click each item to play the corresponding video.

Source code:


Layout file:

Activity_main:

     
      
  
 

List_item:

     
      
  
 

Code file:

MainActivity. java:

Package com. multimediademo13mediastore; import java. io. file; import java. util. arrayList; import android. app. activity; import android. content. intent; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. provider. mediaStore; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. listView; Public class MainActivity extends Activity implements OnItemClickListener {private ListView listView; private Cursor cursor; @ SuppressWarnings ("deprecation") @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) findViewById (R. id. listView);/*** from MediaStore. video. list of columns obtained from the Thumbnail query. */String [] thumbColumns = {MediaStore. video. thumbnails. DATA, MediaStore. video. thumbnails. VIDEO_ID};/*** from MediaStore. video. the list of columns in Media query. */String [] mediaColumns = {MediaStore. video. media. _ ID, MediaStore. video. media. DATA, MediaStore. video. media. TITLE, MediaStore. video. media. MIME_TYPE};/*** in the main query, all videos in MediaStore are selected */cursor = managedQuery (MediaStore. video. media. EXTERNAL_CONTENT_URI, mediaColumns, null); ArrayList
 
  
VideoRows = new ArrayList
  
   
(); If (cursor. moveToFirst () {do {VideoViewInfo newVVI = new VideoViewInfo ();/*** extracts thumbnails for each video using another query, these data blocks are stored in the VideoViewInfo object. */Int id = cursor. getInt (cursor. getColumnIndex (MediaStore. video. media. _ ID); Cursor thumbCursor = managedQuery (MediaStore. video. thumbnails. EXTERNAL_CONTENT_URI, thumbColumns, MediaStore. video. thumbnails. VIDEO_ID + "=" + id, null, null); if (thumbCursor. moveToFirst () {newVVI. thumbPath = thumbCursor. getString (thumbCursor. getColumnIndex (MediaStore. video. thumbnails. DATA);} newVVI. filePath = cursor. getStr Ing (cursor. getColumnIndexOrThrow (MediaStore. video. media. DATA); newVVI. title = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. video. media. TITLE); newVVI. mimeType = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. video. media. MIME_TYPE); videoRows. add (newVVI);} while (cursor. moveToNext ();} listView. setAdapter (new VideoGalleryAdapter (this, videoRows); listView. setOnItemClickListener (This);}/*** this method extracts the required data from the Cursor object and click an item to create an intent, to start the default media player on the mobile phone device to play the item video. * // @ Overridepublic void onItemClick (AdapterView
   Parent, View view, int position, long id) {if (cursor. moveToPosition (position) {int fileColumn = cursor. getColumnIndexOrThrow (MediaStore. video. media. DATA); int mimeColumn = cursor. getColumnIndexOrThrow (MediaStore. video. media. MIME_TYPE); String videoFilePath = cursor. getString (fileColumn); String mimeType = cursor. getString (mimeColumn); Intent intent = new Intent (android. content. intent. ACTION_VIEW); File newFile = new File (videoFilePath); intent. setDataAndType (Uri. fromFile (newFile), mimeType); startActivity (intent );}}}
  
 

VideoViewInfo. java:

package com.multimediademo13mediastore;public class VideoViewInfo {String filePath;String mimeType;String thumbPath;String title;}
VideoGalleryAdapter. java:

Package com. multimediademo13mediastore; import java. util. arrayList; import android. content. context; import android.net. uri; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. textView;/*** ListView adapter ** @ author Administrator **/public class VideoGalleryAdapter extends BaseAdapter {private Context context; private ArrayList
 
  
VideoRows; LayoutInflater inflater; public VideoGalleryAdapter (Context context, ArrayList
  
   
VideoRows) {this. context = context; this. videoRows = videoRows; inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE);} @ Overridepublic int getCount () {return videoRows. size () ;}@ Overridepublic Object getItem (int position) {return null ;}@ Overridepublic long getItemId (int position) {return 0 ;}@ Overridepublic View getView (int position, view convertView, ViewGroup parent ){ View videoRow = inflater. inflate (R. layout. list_item, null); ImageView videoThumb = (ImageView) videoRow. findViewById (R. id. imageView); if (videoRows. get (position ). thumbPath! = Null) {videoThumb. setImageURI (Uri. parse (videoRows. get (position ). thumbPath);} TextView videoTitle = (TextView) videoRow. findViewById (R. id. textView); videoTitle. setText (videoRows. get (position ). title); return videoRow ;}}
  
 

Download source code:

Click to download source code


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.