2011-05-06 11 views
6

내 프로그램에서 선택한 벨소리를 파일 경로로 저장 한 다음 나중에 현재 벨소리로 설정하려고합니다.파일 경로를 통해 MediaStore에서 Uri를 얻는 방법은 무엇입니까?

나는 RingtonePreference에서 벨소리 URI를 얻었으며 MediaStore 데이터베이스에서 파일 경로를 가져온다.

이제 어떻게 저장 한 파일 경로에서 벨소리를 얻을 수 있습니까?

벨소리가 이미 MediaStore에 있기 때문에 다음 기능을 시도했지만 작동하지 않습니다.

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

Uri는 RingtonePreference에서 가져온 것과 다릅니다.

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

내가 필요로하는 Uri를 얻기 위해 MediaStore를 어떻게 쿼리합니까?

p.s. 내가 벨소리를 직접 저장하지 않는 이유는 같은 벨소리에 대해 Uri가 어떤 장치에서 때때로 바뀔지도 모른다는 사실을 발견했기 때문입니다.

+0

어느 하나 ?? 감사. – dong221

답변

5

RingtonePreference에 저장된 벨소리 URI를 복구 할 수있는 방법은 노래의 제목을 아는 것입니다 (아는 한). 그런 다음 저장된 벨소리 _id를 얻기 위해 커서를 사용하여 조회 할 수 그것으로 당신은 URI를 구축 할 수 있습니다 :

finalSuccessful URI가 RingtonePreference에서 벨소리로 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(); 
} 

.

+0

실제로'MediaStore.Audio.Media.EXTERNAL_CONTENT_URI' 또는'MediaStore.Audio.Media.INTERNAL_CONTENT_URI'를 사용하여 루트 URI에 액세스하는 것이 더 좋습니다. –

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 (등)를 대체하여 오디오 또는 파일 또는 저장된 콘텐츠를 다른 종류의 사용할 수있는 후자 :

을 당신이 정보가 그것을 보는 것을 사용하므로 기본적으로

/** 
* 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로 a 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; 
    } 
관련 문제