2012年4月7日星期六

使用managedQuery()獲取Android設備的視頻媒體內容

本程序演示如何使用managedQuery()方法, 從媒體存儲(MediaStore)獲取Android設備的視頻媒體內容. 把Uri設定為 MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 排序設定為 MediaStore.Video.Media.TITLE.

使用managedQuery()獲取Android設備的視頻媒體內容

package com.AndroidMediaList;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class AndroidMediaListActivity extends ListActivity {

//For Video
Uri targetUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String sortOrder = MediaStore.Video.Media.TITLE;
//For Audio
//Uri targetUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//String sortOrder = MediaStore.Audio.Media.TITLE;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String[] from = {MediaStore.MediaColumns.TITLE};
int[] to = {android.R.id.text1};

Cursor cursor = managedQuery(targetUri, null, null, null, sortOrder);

ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);
setListAdapter(adapter);
}
}

沒有留言:

發佈留言