How to set ringtone in Android from my activity?
Asked Answered
A

13

46

I'm trying to find a way to set a new default ringtone by code from my Android activity.

I have already downloaded the ringtone into a bytearray.

Acolyte answered 13/8, 2009 at 12:50 Comment(0)
A
73

Finally, I managed to set the default ringtone to one that i downloaded. The download code is not included below, only what was needed to set it as default ringtone.

File k = new File(path, "mysong.mp3"); // path is a file to /sdcard/media/ringtone

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
values.put(MediaStore.Audio.Media.DURATION, 230);
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(k.getAbsolutePath());
Uri newUri = this.getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
  myActivity,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);  

Anyway, I do not totally understand what this code is doing.

The Ringtone manager needs a uri to the file that is to be set as new ringtone. But this uri can not be directly to the sdcard like "/sdcard/media/ringtones/mysong.mp3". That does not work!

What you need is the external file uri of the file which could be something like "/external/audio/media/46"

The 46 is the id of the column in the MediaStore database, so thats why you need to add the sdcard file into the database first.

Anyway, how does mediastore maintain its ids? This number can get really high, as you do this operation many times.

Do i need to delete this row my self? Problem is that some times i dont even controll the deleting of the file since it can be deleted directly from the sdcard with a filebrowser.

Acolyte answered 17/8, 2009 at 11:21 Comment(7)
Interacting with the media content provider in the code above creates a new entry each time. You may want to keep track of the URIs you are generating, so you can avoid re-downloading, and skip right to RingtoneManager.Interclavicle
what is main in this line Uri newUri = main.getContentResolver().insert(uri, values); I am getting new Uri as null why I don't no. Please help me..Razee
I used getApplicationContext() in place of main is it right or what else I can use to solve this issue..Razee
Works fine in Android L. But it kitkat it gives newUri as null. I see following Exception android.database.sqlite.SQLiteConstraintException: column _data is not unique.And this is happening for any audio file I add.Bodwell
add below line of code before newUri getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"", null);Sing
I want to set default ringtone as none/silence how can I go for this using your code.please help...Kaleidoscopic
Do you have any idea how to set Text as ringtone pls help me here like for a single contactGillead
S
4

Answer By Vidar is too long and it adds duplicate entries every time you want to set a song as ringtone . Instead you should try this

Uri newUri=Uri.parse("content://media/external/audio/media/"+ID);  
try {
      RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
    }
catch (Throwable t) {


                  }
Squishy answered 26/8, 2016 at 7:24 Comment(2)
You need a combination of Vidar's answer and your's here, which should be run when the file already exists on the SD card.Defibrillator
If we have the id of the audio file,we could use this instead , Uri uri; if(path_to_the_file.startsWith("/system")){ uri = Uri.parse(MediaStore.Audio.Media.INTERNAL_CONTENT_URI.toString() +"/"+ id); } else uri = Uri.parse(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString() +"/"+ id);Ariannaarianne
J
3
public void setRingtone() {
  String ringtoneuri = Environment.getExternalStorageDirectory().getAbsolutePath() + "/media/ringtone";
  File file1 = new File(ringtoneuri);
  file1.mkdirs();
  File newSoundFile = new File(ringtoneuri, "myringtone.mp3");


  Uri mUri = Uri.parse("android.resource://globalapps.funnyringtones/raw/sound_two.mp3");


  ContentResolver mCr = this.getContentResolver();
  AssetFileDescriptor soundFile;
  try {
   soundFile = mCr.openAssetFileDescriptor(mUri, "r");
  } catch (FileNotFoundException e) {
   soundFile = null;
  }

  try {
   byte[] readData = new byte[1024];
   FileInputStream fis = soundFile.createInputStream();
   FileOutputStream fos = new FileOutputStream(newSoundFile);
   int i = fis.read(readData);

   while (i != -1) {
    fos.write(readData, 0, i);
    i = fis.read(readData);
   }

   fos.close();
  } catch (IOException io) {
  }

  ContentValues values = new ContentValues();
  values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
  values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
  values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
  values.put(MediaStore.MediaColumns.SIZE, newSoundFile.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(newSoundFile.getAbsolutePath());
  Uri newUri = mCr.insert(uri, values);
  try {
   Uri rUri = RingtoneManager.getValidRingtoneUri(this);
   if (rUri != null)
    ringtoneManager.setStopPreviousRingtone(true);
   RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
   Toast.makeText(this, "New Rigntone set", Toast.LENGTH_SHORT).show();
  } catch (Throwable t) {
   Log.e("sanjay in catch", "catch exception"+e.getMessage());
  }

 }
Jori answered 12/1, 2015 at 6:34 Comment(4)
This will help you to set ringtone from raw folder where you have put your all ringtones.It is working fine for me alsoJori
soundFile = mCr.openAssetFileDescriptor(mUri, "r"); can you please tell me Why you have written this line.Aargau
what is globalapps here?Macegan
How can i set it to silent/none as a default ringtone please helpKaleidoscopic
T
2

You can use the built-in RingtonePreference class. AndroidGuys has a nice tutorial on this here.

Tristich answered 13/8, 2009 at 14:52 Comment(2)
Uh, thanks for the shout-out, but RingtonePreference allows the user to choose a ringtone asset for your application to use. It does not set the system ringtone.Bolometer
Ok, may be I missed but the reference does not explicitly tell that. I wonder what would someone do with a ringtone in his application unless the app is handling incoming calls.Tristich
G
1

This is the code i used! i hope it helps..
This is also the link.

 String exStoragePath =    Environment.getExternalStorageDirectory().getAbsolutePath();
String path=(exStoragePath +"/media/alarms/"); 

saveas(RingtoneManager.TYPE_RINGTONE); 

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,       Uri.parse("file://"+path+filename+".mp3"
  + Environment.getExternalStorageDirectory()))); 


 File k = new File(path, filename);

ContentValues values = new ContentValues(4);   
long current = System.currentTimeMillis();
values.put(MediaStore.MediaColumns.DATA, path + filename  );
values.put(MediaStore.MediaColumns.TITLE,  filename ); 
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");

//new
 values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
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
this.getContentResolver()
   .insert(MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath()), values);

HAPPY CODING!

Gesso answered 20/10, 2015 at 8:15 Comment(0)
S
0

I found this code from the Media application from Android.

Settings.System.putString(resolver, 
Settings.System.RINGTONE, ringUri.toString());

this works form my.

Sankaran answered 6/5, 2010 at 17:9 Comment(0)
C
0

I cannot comment the solution because I don't have enough reputation on stack overflow ... I want just add a way to add the audio file into media database without accessing directly to the database and hence avoiding to get duplicates. The solution is based on MediaScannerConnection, this is the code I used:

    String[] files = { audioFullPath };
    MediaScannerConnection.scanFile(
        getApplicationContext(),
        files,
        null,
        new OnScanCompletedListener() {
            @Override
            public void onScanCompleted(String path, Uri uri) {
                Log.v("myapp", "file " + path + " was scanned seccessfully: " + uri);
            }
        }
    );
Contretemps answered 5/1, 2016 at 11:18 Comment(0)
M
0

provide intent for ringtone selection.

final Uri currentTone= RingtoneManager.getActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_ALARM);
                Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentTone);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
                startActivityForResult(intent, 999);

then catch the result of selection in onActivityResult.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 999 && resultCode == RESULT_OK){
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            txtView.setText("From :" + uri.getPath());
            //Set selected ringtone here.
            RingtoneManager.setActualDefaultRingtoneUri(
                    this,
                    RingtoneManager.TYPE_RINGTONE,
                    uri
            );
        }
    }
Marino answered 18/5, 2018 at 15:50 Comment(0)
B
0

I have try these code its help

  private void setRingtone(Context context, String path) {
    if (path == null) {
        return;
    }
    File file = new File(path);
    ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
    String filterName = path.substring(path.lastIndexOf("/") + 1);
    contentValues.put(MediaStore.MediaColumns.TITLE, filterName);
    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    contentValues.put(MediaStore.MediaColumns.SIZE, file.length());
    contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(path);
    Cursor cursor = context.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[]{path}, null);
    if (cursor != null && cursor.moveToFirst() && cursor.getCount() > 0) {
        String id = cursor.getString(0);
        contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        context.getContentResolver().update(uri, contentValues, MediaStore.MediaColumns.DATA + "=?", new String[]{path});
        Uri newuri = ContentUris.withAppendedId(uri, Long.valueOf(id));
        try {
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newuri);
            Toast.makeText(context, "Set as Ringtone Successfully.", Toast.LENGTH_SHORT).show();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        cursor.close();
    }
}
Beitz answered 26/11, 2018 at 10:4 Comment(0)
O
0

If the accepted answer is not working then use this:

MediaStore.Audio.Media.INTERNAL_CONTENT_URI

instead of this:

MediaStore.Audio.Media.getContentUriForPath()

while inserting values into the database.

For example :

// Defining ringtone.....
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Sonify");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
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);

// Setting ringtone....
getContentResolver().delete(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,MediaStore.Audio.Media.TITLE + " = \"Sonify\"",null); 
// To avoid duplicate inserts
Uri ringUri = getContentResolver().insert(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, values);
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, ringUri);

Dont forgot to add

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

Oblique answered 30/9, 2021 at 18:48 Comment(0)
L
0

Use this function to set Ringtone

private void setAsRingtone(String musicId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (Settings.System.canWrite(this)) {
            Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Long.parseLong(musicId));
            RingtoneManager.setActualDefaultRingtoneUri(
                    this,
                    RingtoneManager.TYPE_RINGTONE,
                    uri
            );
            Toast.makeText(this, "Ring set successfully", Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }
}

Music Id can be obtained from Cursor. Hope you know it, or Check it here

Lobworm answered 23/1, 2022 at 12:31 Comment(0)
H
0

That works for me even in android S ,hope its help to you in set ringtone in android programmatically

RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, uri);
Headway answered 2/12, 2022 at 11:42 Comment(0)
T
0

if you create file via your bytearray you can set ringtone with file via this function:

public void setRingtone(String filePath) {
        try {
            File f = new File(filePath);
            f.setReadable(true);
            Uri newUri = Uri.parse(f.toString());
            Settings.System.putString(getContentResolver(), Settings.System.RINGTONE,
                    newUri.toString());

        } catch (Exception t) {
        }
    }

ref. for Settings.System.putString() method to set ringtone: https://android.googlesource.com/platform/frameworks/base/+/master/media/java/android/media/RingtoneManager.java#828

Tacklind answered 25/6, 2023 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.