2011-05-06 18 views
6

मेरे प्रोग्राम में, मैं एक चयनित रिंगटोन को इसके फ़ाइल पथ से सहेजना चाहता हूं, और उसके बाद इसे बाद में रिंगटोन के रूप में सेट करना चाहता हूं।मीडिया पथ से यूरी को फ़ाइल पथ के माध्यम से कैसे प्राप्त करें?

मुझे रिंगटोन यूरी से रिंगटोन यूरी मिली है, और मीडियास्टोर डेटाबेस से इसका फ़ाइल पथ प्राप्त करें।

उदा।

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

अब, मैं सहेजने वाले फ़ाइल पथ से रिंगटोन उरी कैसे प्राप्त करूं?

चूंकि रिंगटोन मीडियास्टोर में पहले से मौजूद है, इसलिए मैंने निम्नलिखित कार्यों का प्रयास किया, लेकिन यह काम नहीं कर रहा है।

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

उरी रिंगटोन प्रेफरेंस से प्राप्त एक जैसा नहीं है।

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

उड़ी मुझे प्राप्त करने के लिए मीडियास्टोर से मैं कैसे पूछूं?

पेज। रिंगटोन उरी को सीधे स्टोर नहीं करने का कारण यह है कि मैंने पाया कि एक ही रिंगटोन के लिए उरी कभी-कभी किसी डिवाइस में बदल सकती है।

+0

कोई भी ?? धन्यवाद। – dong221

उत्तर

5

जिस तरह से आप रिंगटोन यूआरआई में रिंगटोन यूआरआई को संग्रहीत कर सकते हैं, वह गीत के शीर्षक को जानकर (जहां तक ​​मुझे पता है) है। तो फिर तुम यह रिंगटोन _ id संग्रहीत प्राप्त करने के लिए एक कर्सर का उपयोग करके क्वेरी कर सकता है और इसके साथ आप एक यूआरआइ निर्माण कर सकते हैं:

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 RingtonePreference में रिंगटोन uri की ओर इशारा करते है।

+0

रूट यूआरआई तक पहुंचने के लिए वास्तव में 'MediaStore.Audio.Media.EXTERNAL_CONTENT_URI' या 'MediaStore.Audio.Media.INTERNAL_CONTENT_URI' का उपयोग करना बेहतर है। –

3

आप मीडियास्टोर में किसी भी सामग्री के लिए इसे अधिक सामान्य तरीके से भी कर सकते हैं। मुझे यूआरआई से रास्ता प्राप्त करना है और यूआरआई को पथ से प्राप्त करना है। पूर्व:

/** 
* 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; 
} 

असल में, MediaStore की DATA स्तंभ (या जो भी यह आप की क्वेरी रहे हैं की उप-धारा) फ़ाइल पथ संग्रहीत करता है, तो आप का उपयोग करें कि जानकारी यह देखने के लिए अप करने के लिए

1

@ dong221:। आंतरिक यूआरआई उपयोग के रूप में एक MediaStore.Audio.Media.INTERNAL_CONTENT_URI ।

2

निम्नलिखित कोड ऑडियो, वीडियो और छवि की सामग्री उरी के लिए पूर्ण पथ लौटाएगा।

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; 
    } 
संबंधित मुद्दे