Linked Questions

Popular Questions

I have two questions:

  1. When I call my saveas(ressound); (inspired by on a android 4.0.3 device the ringtone is nicely activated as a ringtone, but on a 2.3.3 device the selected ringtone is silent. Where did I go wrong in my code?

  2. The second thing that I want to know is how to delete the file. More importantly, how to get it out of the media store, does this happen automatically if I remove it from the sd?


Added code to delete the whole folder plus content, works good and clean! (see below)

Try to call this code when I push a button:

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

But it will not clean out the mediastore, it works with a reboot. I also tried the media scan within the dev tools, but that doesn't work. They mention that some apps need to restart, but how to restart the service responsible for displaying the list of available ringtones without rebooting the whole device?


String soundname;
int ressound;
int savetype;
String path="/sdcard/mysounds/";

savetype = RingtoneManager.TYPE_RINGTONE;
ressound = R.raw.sound1;
soundname = "sound1";


//save as
  public boolean saveas(int ressound){
     byte[] buffer=null;
     InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
     int size=0;

     try {
      size = fIn.available();
      buffer = new byte[size];;
     } catch (IOException e) {
      return false;

     String filename= soundname +".mp3";

     boolean exists = (new File(path)).exists();
     if (!exists){new File(path).mkdirs();}

     FileOutputStream save;
     try {
      save = new FileOutputStream(path+filename);
     } catch (FileNotFoundException e) {
      return false;
     } catch (IOException e) {
      return false;

     sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));

     File k = new File(path, filename);

     ContentValues values = new ContentValues();
     values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
     values.put(MediaStore.MediaColumns.TITLE, soundname);
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
     values.put(MediaStore.Audio.Media.ARTIST, "Elvis");
     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);

     //Insert it into the database
     Uri newUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);

     // set as ringtone
     RingtoneManager.setActualDefaultRingtoneUri(this, savetype, newUri);

     return true;


public void delete() {
    File a = new File(path, "sound1.mp3");
    File b = new File(path, "sound2.mp3");

deleteFiles(String path);

public static void deleteFiles(String path) {
    File file = new File(path);

    if (file.exists()) {
        String deleteCmd = "rm -r " + path;
        Runtime runtime = Runtime.getRuntime();
        try {
        } catch (IOException e) { }

Related Questions