-1

I have a listview with sounds items. I need, when a item is long press es, set it as ringtone or notification from raw sound of my app. How can I do this? Thanks

user3437592
  • 59
  • 3
  • 8

2 Answers2

0

Answer from my other post here...

Set ringtone from res/raw folder

You cannot set the ringtone from resource.

RingtoneManager.setActualDefaultRingtoneUri expects the ringtone file in the device and the Uri should be from content resolver.

    File ring = new File("pathOfYourFile");
     Uri path = MediaStore.Audio.Media.getContentUriForPath(ring.getAbsolutePath());

RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE,path);

Also make sure to add permission

Community
  • 1
  • 1
Libin
  • 16,618
  • 7
  • 57
  • 81
0

try this code

 String name = "your_raw_audio_name";

 File file = new File(Environment.getExternalStorageDirectory(),
            "/myRingtonFolder/Audio/");
    if (!file.exists()) {
        file.mkdirs();
    }

    String path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/myRingtonFolder/Audio/";

    File f = new File(path + "/", name + ".mp3");

    Uri mUri = Uri.parse("android.resource://"
            + context.getPackageName() + "/raw/" + name);
    ContentResolver mCr = context.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(f);
        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, f.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, name);
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    values.put(MediaStore.MediaColumns.SIZE, f.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, true);

    Uri uri = MediaStore.Audio.Media.getContentUriForPath(f
            .getAbsolutePath());
    Uri newUri = mCr.insert(uri, values);

    try {
        RingtoneManager.setActualDefaultRingtoneUri(context,
                RingtoneManager.TYPE_RINGTONE, newUri);
        Settings.System.putString(mCr, Settings.System.RINGTONE,
                newUri.toString());
    } catch (Throwable t) {

    }

of more details check this link

Community
  • 1
  • 1
wael
  • 434
  • 4
  • 11