Android Mediastore Retrieve video and play

Source: Internet
Author: User

This article is to retrieve the video from the SD card on the phone, and then display the video information such as the thumbnail, name, etc. of the corresponding video on the ListView. Click each item to play the corresponding video.

Source:


Layout file:

Activity_main:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <listview        android:id= "@+id/listview"        android:layout_width= "Match_parent"        android: layout_height= "Wrap_content" >    </ListView></RelativeLayout>

List_item:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <imageview        android:id= "@+id/imageview"        android:layout_width= "Wrap_content"        android: layout_height= "Wrap_content"/>    <textview        android:id= "@+id/textview" android:layout_width= "        Wrap_content "        android:layout_height=" Wrap_content "/></linearlayout>

Code files:

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);/** * The list of columns obtained from the MediaStore.Video.Thumbnail query. */string[] Thumbcolumns = {mediastore.video.thumbnails.data,mediastore.video.thumbnails.video_id};/** * The list of columns obtained from the MediaStore.Video.Media query. */string[] Mediacolumns = {Mediastore.video.media._id,mediastore.video.Media.data, Mediastore.video.media.title,mediastore.video.media.mime_type};/** * All videos represented in the MediaStore will be selected in the main query */ cursor = managedquery (mediastore.video.media.external_content_uri,mediacolumns, NULL, NULL, NULL); arraylist<videoviewinfo> videorows = new arraylist<videoviewinfo> (); if (Cursor.movetofirst ()) {do { Videoviewinfo Newvvi = new Videoviewinfo ();/** * Extracts thumbnails for each video using another query, and the 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.getstring (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 clicks on an item that will create an intent to start the default media player on the mobile 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& Lt Videoviewinfo> videorows; Layoutinflater inflater;public videogalleryadapter (Context context,arraylist<videoviewinfo> 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;}}

SOURCE Download:

Click to download the source code


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.