Set a selected audio file as ringtone

noobProgrammer picture noobProgrammer · Feb 13, 2014 · Viewed 18.1k times · Source

I am developing an application: I am trying to set an audio file as a ringtone.
I saw many posts, but no one actually helped me, so I decided to ask this question.

I use this code when the Button is clicked:

@Override
    public void onClick(View v) {
Intent intent = new Intent();  
            intent.setAction(Intent.ACTION_GET_CONTENT);  
            intent.setType("audio/*");          
            startActivityForResult(Intent.createChooser(intent, "Choose Sound File"), Audio);
}

in onActivityResult I am trying to get the file path and then set the audio file as ringtone using the code below:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Audio && requestCode==RESULT_OK) {


          Uri s1 = data.getData();
          String s = s1.getPath(); 


        if(s!=null){    

            try {
                k = new File(new URI(s)); //(File k;)
            } catch (URISyntaxException e) {

                e.printStackTrace();
            }
                ContentValues values = new ContentValues();
                values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
                values.put(MediaStore.MediaColumns.TITLE, "My Song title");
                values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
                values.put(MediaStore.Audio.Media.ARTIST, "Some Artist");
                values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
                values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                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(k.getAbsolutePath());
                Uri newUri = getContentResolver().insert(uri, values);

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

        }

        }
}

Unfortunately this code doesn't work.
I would appreciate your answers. Sorry for my bad English.

Answer

noobProgrammer picture noobProgrammer · Feb 23, 2014

I saw many posts but anyone showed what i should actually have to do. So i decided to create this complete answer , in which i have the solution of my problem...

Here is my MainActivity.java which i used

import java.io.File;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    Button b;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        b = (Button) findViewById(R.id.button2);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                Intent intent1 = new Intent();
                intent1.setAction(Intent.ACTION_GET_CONTENT);
                intent1.setType("audio/*");
                startActivityForResult(
                    Intent.createChooser(intent1, "Choose Sound File"), 6);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == 6) {
            Uri i = data.getData();  // getData
            String s = i.getPath(); // getPath
            File k = new File(s);  // set File from path
            if (s != null) {      // file.exists

                ContentValues values = new ContentValues();
                values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
                values.put(MediaStore.MediaColumns.TITLE, "ring");
                values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                values.put(MediaStore.MediaColumns.SIZE, k.length());
                values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
                values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                values.put(MediaStore.Audio.Media.IS_ALARM, true);
                values.put(MediaStore.Audio.Media.IS_MUSIC, false);

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

                try {
                    RingtoneManager.setActualDefaultRingtoneUri(
                        MainActivity.this, RingtoneManager.TYPE_RINGTONE,
                        newUri);
                } catch (Throwable t) {

                }
            }
        }
    }
}

Lastly, its really important to add those permissions in your AndroidManifest.xml for example if you don't add the permission to write external storage your app will crash like mine.. xD

What you need:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

You can try my app on Google Play : BackAtel Audio Manager

Hope that helps.... my problem is now solved!! i hope that i solved your problem too :))