Setting tone as ringtone/notification in Android 11 / R

353 Views Asked by At

I am trying to set ringtone / notification tone in Android 11 since last 4-5 days, but doesn't able to complete it, It's not like i haven't tried anything new or something that, I have code which is already working fine in Android 10 and below version but when i set target to Android 30, It doesn't work.

public boolean saveAsRingtone() {
    if (mInterstitialAd != null) {
        mInterstitialAd.show(this);
    }
    InputStream fIn = getBaseContext().getResources().openRawResource(R.raw.file);
    try {
        byte[] buffer = new byte[fIn.available()];
        fIn.read(buffer);
        fIn.close();

        String path = Environment.getExternalStorageDirectory() + "/Sounds/Media/";
        String filename = "Ring.mp3";
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        try {
            FileOutputStream save = new FileOutputStream(path + filename);
            save.write(buffer);
            save.flush();
            save.close();
            sendBroadcast(new Intent("android.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, "RingTone");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.MediaColumns.SIZE, 215454);
            values.put(MediaStore.MediaColumns.ARTIST, "Test");
            if (RingtoneManager.TYPE_RINGTONE == RingtoneManager.TYPE_RINGTONE) {
                values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            } else if (RingtoneManager.TYPE_NOTIFICATION == RingtoneManager.TYPE_NOTIFICATION) {
                values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
            }
            values.put(MediaStore.Audio.Media.IS_ALARM, Boolean.valueOf(false));
            values.put(MediaStore.Audio.Media.IS_MUSIC, Boolean.valueOf(false));


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

            RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, uri);
            Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_SHORT).show();
                return true;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e2) {
            e2.printStackTrace();
            return false;
        }
    } catch (IOException e3) {
        e3.printStackTrace();
        return false;
    }
}

I already know about scoped storage and other permission related things which i have already declared in manifest file and so on.

0

There are 0 best solutions below