AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
public void setAsRingTone(){
String filePath = getSelectedFilePath();
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(filePath);
String durationStr = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
int duration = (int)Long.parseLong(durationStr);
metaRetriever.release();
File ringtoneFile = new File(filePath);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "ocsoosoo");
values.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "ocsoosoo");
values.put(MediaStore.Audio.Media.DURATION, duration);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile.getAbsolutePath());
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
Toast.makeText(this, "Completed.", Toast.LENGTH_SHORT).show();
}
public void setAsAlarmTone(){
String filePath = getSelectedFilePath();
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(filePath);
String durationStr = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
int duration = (int)Long.parseLong(durationStr);
metaRetriever.release();
File ringtoneFile = new File(filePath);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "ocsoosoo");
values.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "ocsoosoo");
values.put(MediaStore.Audio.Media.DURATION, duration);
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile.getAbsolutePath());
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, newUri);
Toast.makeText(this, "Completed.", Toast.LENGTH_SHORT).show();
}
'programmer > android' 카테고리의 다른 글
popupwindow example (0) | 2013.07.03 |
---|---|
usb event broadcastreceiver (0) | 2013.07.03 |
seekbar progress 위치에 value 표시 (0) | 2013.06.25 |
android custom actionbar (0) | 2013.06.22 |
dialog position (0) | 2013.06.20 |