Skip to content

Instantly share code, notes, and snippets.

@ufo22940268
Created November 1, 2012 09:02
Show Gist options
  • Save ufo22940268/3992587 to your computer and use it in GitHub Desktop.
Save ufo22940268/3992587 to your computer and use it in GitHub Desktop.
public static Uri convertToRingtoneUri(Context context, String customRingtone) {
if (TextUtils.isEmpty(customRingtone) || SILENT_RINGTONE_LABEL.equals(customRingtone)) {
return null;
}
//Query internal media.
ContentResolver cr = context.getContentResolver();
Cursor c = cr.query(Media.INTERNAL_CONTENT_URI,
new String[]{Media._ID},
Media.DATA + " = ?",
new String[]{customRingtone},
null);
Uri uri = null;
if (c != null) {
if (c.moveToFirst()) {
long id = c.getLong(0);
uri = ContentUris.withAppendedId(Media.INTERNAL_CONTENT_URI, id);
}
c.close();
}
//Query external media.
if (uri == null) {
c = cr.query(Media.EXTERNAL_CONTENT_URI,
new String[]{Media._ID},
Media.DATA + " = ?",
new String[]{customRingtone},
null);
if (c != null) {
if (c.moveToFirst()) {
long id = c.getLong(0);
uri = ContentUris.withAppendedId(Media.EXTERNAL_CONTENT_URI, id);
}
c.close();
}
}
if (uri != null) {
return uri;
} else {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment