1

Currently I can set ringtones and notifications with:

    byte[] buffer = null;
    InputStream fIn = getBaseContext().getResources().openRawResource(
            ressound);
    int size = 0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        return false;
    }

    String path = "/sdcard/TEST/";
    String filename = MediaStore.MediaColumns.TITLE + ".mp3";

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

    FileOutputStream save;
    try {
        save = new FileOutputStream(path + filename);
        save.write(buffer);
        save.flush();
        save.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        return false;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        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, "TEST:RingTone");
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    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, false);
    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);

    RingtoneManager.setActualDefaultRingtoneUri(this,
            RingtoneManager.TYPE_RINGTONE, newUri);
    Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show();
    return true;

The problem is the program it continues to add the same TEST:Notification each time a new notification (or ringtones) is set versus only adding one. I think it has to do this this line:

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

But I'm not sure how to set a check to see if the file name has already been created in the Android system notification spinner.

Nick
  • 9,285
  • 33
  • 104
  • 147

1 Answers1

1

I found the answer here and the code that worked is:

    Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
    getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
    Uri newUri = getContentResolver().insert(uri, values);
Community
  • 1
  • 1
Nick
  • 9,285
  • 33
  • 104
  • 147