Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android MediaStore檢索視頻並播放

Android MediaStore檢索視頻並播放

編輯:關於Android編程

該文章是為了檢索手機上sd卡中的視頻,然後將檢索出來的相應視頻的縮略圖,名稱等視頻信息顯示在ListView上。點擊每個item後播放相應的視頻。

源代碼:

\

布局文件:

activity_main:



    
    


list_item:



    

    


代碼文件:

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")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		listView = (ListView) findViewById(R.id.listView);
		/**
		 * 從MediaStore.Video.Thumbnail查詢中獲得的列的列表。
		 */
		String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,
				MediaStore.Video.Thumbnails.VIDEO_ID };
		/**
		 * 從MediaStore.Video.Media查詢中獲得的列的列表。
		 */
		String[] mediaColumns = { MediaStore.Video.Media._ID,
				MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
				MediaStore.Video.Media.MIME_TYPE };
		/**
		 * 在主查詢中將選擇所有在MediaStore中表示的視頻
		 */
		cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				mediaColumns, null, null, null);
		ArrayList videoRows = new ArrayList();
		if (cursor.moveToFirst()) {
			do {
				VideoViewInfo newVVI = new VideoViewInfo();
				/**
				 * 將使用另一個查詢為每個視頻提取縮略圖,而且這些數據塊都將存儲在VideoViewInfo對象中。
				 */
				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);
	}

	/**
	 * 這個方法將從Cursor對象中提取所需的數據,點擊一個item,將創建一個意圖,以啟動手機設備上默認的媒體播放器來播放該item視頻。
	 */
	@Override
	public 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適配器
 * @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);
	}

	@Override
	public int getCount() {
		return videoRows.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public 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;
	}

}

源碼下載:

點擊下載源碼


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved