編輯:關於Android編程
該文章是為了檢索手機上sd卡中的視頻,然後將檢索出來的相應視頻的縮略圖,名稱等視頻信息顯示在ListView上。點擊每個item後播放相應的視頻。
源代碼:
布局文件:
activity_main:
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); ArrayListvideoRows = 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); } } }
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 ArrayListvideoRows; 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; } }
源碼下載:
點擊下載源碼
這是一個一言不合就手撸一個自定義View的任性時代,因此最近一段時間一直在學習自定義View相關的知識,也看了很多與此相關的博客,有句話叫做不要重復造輪子,別人寫好的直接
1、ctrl + shift + z 我想就連沒編過程序的人都知道 ctrl + z 是回退鍵,但是很少人知道 這個“前進鍵”吧,事實上這個快捷鍵
動態加載、插件化開發很重要當今360手機助手(DroidPlugin),個人開源(VirtualApp)、百度DL、攜程DynamicAPK都用到了該技術本例的大概思路是
轉載請標明出處:http://blog.csdn.net/lmj623565791/article/details/39480503,本文出自:【張鴻洋的博客】上一篇已經