user3231871 user3231871 - 20 days ago 6
Android Question

Set as Ringtone android

first I want to say that I'm sorry I know there are already questions about this, but I am new to programing and android is my first language.
How can I set my application to set sound from my app as ringtone?
I got this

String filepath ="/sdcard/yeahitsworking";
File ringtoneFile = new File(filepath);

ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, "chinnu");
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
content.put(MediaStore.Audio.Media.IS_ALARM, false);
content.put(MediaStore.Audio.Media.IS_MUSIC, false);


//Insert it into the database
Log.i(TAG, "the absolute path of the file is :"+
ringtoneFile.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile.getAbsolutePath());
//Uri newUri = context.getContentResolver().insert(uri, content);
Uri newUri = getContentResolver().insert(uri, content);
String ringtoneUri = newUri.toString();

Log.i(TAG,"the ringtone uri is :"+ringtoneUri);
RingtoneManager.setActualDefaultRingtoneUri(getBaseContext(),RingtoneManager.TYPE_RINGTONE,newUri);


How can I set this so when I click on button Eg. bt1 to set this as ringtone?
Is it something like this

ImageButton bt1 = (ImageButton) findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {


This is not a double post I've searched already and non of them answers my question.

Answer

You can set your Ringtone on click event of your button.

   urbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



            String filepath ="/sdcard/yeahitsworking";
            File ringtoneFile = new File(filepath);

            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, ringtoneFile .getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "My Song title");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
            values.put(MediaStore.MediaColumns.SIZE, 215454);
            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);

            Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile 
                    .getAbsolutePath());
            getContentResolver().delete(
                    uri,
                    MediaStore.MediaColumns.DATA + "=\""
                            + ringtoneFile .getAbsolutePath() + "\"", null);
            Uri newUri = getContentResolver().insert(uri, values);

            RingtoneManager.setActualDefaultRingtoneUri(
                    YourActivityName.this, RingtoneManager.TYPE_RINGTONE,
                    newUri);

        }
    });