androidtitleandroid-10.0ringtone

set custom ring tone in Android Api 29


I have a kotlin function which sets recorded audio as my ring tone. It copies the sound file to ring tone library and i can play it. The issue is 1 - It sets title as 1617159412418 instead of the string title i specified in content values 2 - it is not selecting that ringtone as default

I use Android API 29 phone (samsung M02s) Please let me know what could be the issue ————code ————

fun setTone() {
    //recorded sound file
    val inputfile = getAudioFile() + ".wav"

    val f = File(inputfile)


    val value = ContentValues()
    value.put(MediaColumns.TITLE, f.name)
    value.put(MediaColumns.SIZE, f.length())
    value.put(MediaColumns.MIME_TYPE, "audio/*")
    value.put(MediaStore.Audio.Media.ARTIST, "jothiring")
    value.put(MediaStore.Audio.Media.DURATION, 500)
    value.put(MediaStore.Audio.Media.IS_ALARM, false)
    value.put(MediaStore.Audio.Media.IS_MUSIC, false)
    value.put(MediaStore.Audio.Media.IS_NOTIFICATION, false)
    value.put(MediaStore.Audio.Media.IS_RINGTONE, true)
    val cr: ContentResolver = contentResolver

    val newUri: Uri? = cr.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, value)

    if (newUri != null) {
        val inputstream = FileInputStream(inputfile)
        val outputstream = cr.openOutputStream(newUri)
        val buffer = ByteArray(1024)
        var read: Int = inputstream.read(buffer)

        while(read != -1) {
            outputstream?.write(buffer, 0, read)
            read = inputstream.read(buffer)
        }
        inputstream.close()
        outputstream?.close()

            //set default ringtone
        RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri!!)


    }

} //end setTone

Solution

  • fill up the MediaStore.Audio.Media.DISPLAY_NAME value in the contentvalues and then insert