2011-05-06 137 views
6

在我的程序中,我想通過它的文件路徑保存選定的鈴聲,然後將其設置爲當前的鈴聲。如何通過文件路徑從MediaStore獲取Uri?

我已經從RingtonePreference獲得了ringtone uri,並從MediaStore數據庫獲取它的文件路徑。

例如

 
Uri - content://media/internal/audio/media/29 
Path - /system/media/audio/notifications/Ascend.mp3 

現在,如何從我保存的文件路徑中獲取鈴聲Uri?

由於MediaStore中已經存在鈴聲,我嘗試了以下功能,但它不起作用。

 
uriRingtone = MediaStore.Audio.Media.getContentUriForPath(szRingtonePath); 

Uri與我從RingtonePreference獲得的不一樣。

 
uriRingtone - content://media/internal/audio/media 

如何查詢MediaStore以獲取我需要的Uri?

p.s.我不直接存儲鈴聲Uri的原因是我發現相同鈴聲的Uri有時可能會在某些設備中更改。

+0

任何一個??謝謝。 – dong221 2011-05-08 15:19:47

回答

5

通過了解歌曲的標題,您可以恢復存儲在RingtonePreference中的鈴聲URI的方式(據我所知)。然後,你可以使用光標獲得存儲鈴聲_id查詢它,並與它,你可以建立一個URI:

String ringtoneTitle = "<The desired ringtone title>"; 
Uri parcialUri = Uri.parse("content://media/external/audio/media"); // also can be "content://media/internal/audio/media", depends on your needs 
Uri finalSuccessfulUri; 

RingtoneManager rm = new RingtoneManager(getApplicationContext()); 
Cursor cursor = rm.getCursor(); 
cursor.moveToFirst(); 

while(!cursor.isAfterLast()) { 
    if(ringtoneTitle.compareToIgnoreCase(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE))) == 0) { 
    int ringtoneID = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID)); 
     finalSuccessfulUri = Uri.withAppendedPath(parcialUri, "" + ringtoneID); 
     break; 
    } 
    cursor.moveToNext(); 
} 

其中finalSuccessful URI是URI指向在RingtonePreference鈴聲。

+0

實際上,使用MediaStore.Audio.Media.EXTERNAL_CONTENT_URI或MediaStore.Audio.Media.INTERNAL_CONTENT_URI來訪問根URI可能會更好。 – 2016-01-23 23:50:31

3

您也可以對MediaStore中的任何內容以更通用的方式執行此操作。我必須從URI獲取路徑並從路徑獲取URI。前者:

/** 
* Gets the corresponding path to a file from the given content:// URI 
* @param selectedVideoUri The content:// URI to find the file path from 
* @param contentResolver The content resolver to use to perform the query. 
* @return the file path as a string 
*/ 
private String getFilePathFromContentUri(Uri selectedVideoUri, 
     ContentResolver contentResolver) { 
    String filePath; 
    String[] filePathColumn = {MediaColumns.DATA}; 

    Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null); 
    cursor.moveToFirst(); 

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
    filePath = cursor.getString(columnIndex); 
    cursor.close(); 
    return filePath; 
} 

後者(這是我對影片做的,但也可以通過對MediaStore.Video代MediaStore.Audio(ETC),用於音頻或文件或其他類型的存儲內容:

/** 
* Gets the MediaStore video ID of a given file on external storage 
* @param filePath The path (on external storage) of the file to resolve the ID of 
* @param contentResolver The content resolver to use to perform the query. 
* @return the video ID as a long 
*/ 
private long getVideoIdFromFilePath(String filePath, 
     ContentResolver contentResolver) { 


    long videoId; 
    Log.d(TAG,"Loading file " + filePath); 

      // This returns us content://media/external/videos/media (or something like that) 
      // I pass in "external" because that's the MediaStore's name for the external 
      // storage on my device (the other possibility is "internal") 
    Uri videosUri = MediaStore.Video.Media.getContentUri("external"); 

    Log.d(TAG,"videosUri = " + videosUri.toString()); 

    String[] projection = {MediaStore.Video.VideoColumns._ID}; 

    // TODO This will break if we have no matching item in the MediaStore. 
    Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null); 
    cursor.moveToFirst(); 

    int columnIndex = cursor.getColumnIndex(projection[0]); 
    videoId = cursor.getLong(columnIndex); 

    Log.d(TAG,"Video ID is " + videoId); 
    cursor.close(); 
    return videoId; 
} 

基本上,MediaStoreDATA列(或任何它要查詢的子部分)存儲的文件路徑,讓你用這些信息來關注一下吧

1

@ dong221:使用URI作MediaStore.Audio.Media.INTERNAL_CONTENT_URI 。

2

以下代碼將返回音頻,視頻和圖像的內容Uri的絕對路徑。

public static String getRealPathFromURI(Context context, Uri contentUri) { 
     Cursor cursor = context.getContentResolver().query(contentUri, null, null, null, null); 

     int idx; 
     if(contentUri.getPath().startsWith("/external/image") || contentUri.getPath().startsWith("/internal/image")) { 
      idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
     } 
     else if(contentUri.getPath().startsWith("/external/video") || contentUri.getPath().startsWith("/internal/video")) { 
      idx = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA); 
     } 
     else if(contentUri.getPath().startsWith("/external/audio") || contentUri.getPath().startsWith("/internal/audio")) { 
      idx = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA); 
     } 
     else{ 
      return contentUri.getPath(); 
     } 
     if(cursor != null && cursor.moveToFirst()) { 
      return cursor.getString(idx); 
     } 
     return null; 
    }