2016-03-07 51 views
1

我是android编程中的新手。我试图找出如何从this从内容解析器中检索媒体

ContentResolver contentResolver = getContentResolver(); 
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
Cursor cursor = contentResolver.query(uri, null, null, null, null); 
if (cursor == null) { 
    // query failed, handle error. 
} else if (!cursor.moveToFirst()) { 
    // no media on the device 
} else { 
    int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE); 
    int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID); 
    do { 
     long thisId = cursor.getLong(idColumn); 
     String thisTitle = cursor.getString(titleColumn); 
     // ...process entry... 
    } while (cursor.moveToNext()); 
} 

我不知道如何处理项目,或者做什么用的代码的另一块,他们给了使用的代码。

long id = /* retrieve it from somewhere */; 
Uri contentUri = ContentUris.withAppendedId(
     android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id); 

mMediaPlayer = new MediaPlayer(); 
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
mMediaPlayer.setDataSource(getApplicationContext(), contentUri); 

// ...prepare and start... 

有人可以告诉我它是如何工作的以及如何检索数据。

回答

0

我迟到了,但希望它会帮助你here是如何使用内容提供商使用内容提供商

import android.content.ContentResolver; 
import android.content.ContentUris; 
import android.database.Cursor; 
import android.net.Uri; 
import android.provider.MediaStore; 
import android.util.Log; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 

public class MusicRetriever { 
final String TAG = "MusicRetriever"; 
ContentResolver mContentResolver; 
// the items (songs) we have queried 
List<Item> mItems = new ArrayList<Item>(); 
Random mRandom = new Random(); 
public MusicRetriever(ContentResolver cr) { 
    mContentResolver = cr; 
} 
/** 
* Loads music data. This method may take long, so be sure to call it asynchronously without 
* blocking the main thread. 
*/ 
public void prepare() { 
    Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    Log.i(TAG, "Querying media..."); 
    Log.i(TAG, "URI: " + uri.toString()); 
    // Perform a query on the content resolver. The URI we're passing specifies that we 
    // want to query for all audio media on external storage (e.g. SD card) 
    Cursor cur = mContentResolver.query(uri, null, 
      MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null); 
    Log.i(TAG, "Query finished. " + (cur == null ? "Returned NULL." : "Returned a cursor.")); 
    if (cur == null) { 
     // Query failed... 
     Log.e(TAG, "Failed to retrieve music: cursor is null :-("); 
     return; 
    } 
    if (!cur.moveToFirst()) { 
     // Nothing to query. There is no music on the device. How boring. 
     Log.e(TAG, "Failed to move cursor to first row (no query results)."); 
     return; 
    } 
    Log.i(TAG, "Listing..."); 
    // retrieve the indices of the columns where the ID, title, etc. of the song are 
    int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST); 
    int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE); 
    int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM); 
    int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION); 
    int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID); 
    Log.i(TAG, "Title column index: " + String.valueOf(titleColumn)); 
    Log.i(TAG, "ID column index: " + String.valueOf(titleColumn)); 
    // add each song to mItems 
    do { 
     Log.i(TAG, "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn)); 
     mItems.add(new Item(
       cur.getLong(idColumn), 
       cur.getString(artistColumn), 
       cur.getString(titleColumn), 
       cur.getString(albumColumn), 
       cur.getLong(durationColumn))); 
    } while (cur.moveToNext()); 
    Log.i(TAG, "Done querying media. MusicRetriever is ready."); 
} 
public ContentResolver getContentResolver() { 
    return mContentResolver; 
} 
/** Returns a random Item. If there are no items available, returns null. */ 
public Item getRandomItem() { 
    if (mItems.size() <= 0) return null; 
    return mItems.get(mRandom.nextInt(mItems.size())); 
} 
public static class Item { 
    long id; 
    String artist; 
    String title; 
    String album; 
    long duration; 
    public Item(long id, String artist, String title, String album, long duration) { 
     this.id = id; 
     this.artist = artist; 
     this.title = title; 
     this.album = album; 
     this.duration = duration; 
    } 
    public long getId() { 
     return id; 
    } 
    public String getArtist() { 
     return artist; 
    } 
    public String getTitle() { 
     return title; 
    } 
    public String getAlbum() { 
     return album; 
    } 
    public long getDuration() { 
     return duration; 
    } 
    public Uri getURI() { 
     return ContentUris.withAppendedId(
       android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id); 
    } 
} 

希望检索数据的很好的例子,这将帮助你