0

The question says it all, i have a function to which i pass a phone number and the function asigns a predownloaded ringtone to a contact with the passed phone number (if such a contact exists). The problem is that it only works on Android 9 and below, when i run it on an Android 10 it just does nothing, no exceptions, no crashes, nothing..

Heres the functions if anybody knows whats up:

private fun setCustomRingtone(targetContactPhone: String): String {

    val lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, targetContactPhone)
    val projection = arrayOf<String>(Contacts._ID, Contacts.LOOKUP_KEY)
    
    val data = contentResolver.query(lookupUri, projection, null, null,null)!!
    data.moveToFirst()

    try {
        // Get the contact lookup Uri
        val contactID: Long = data.getLong(0)
        val lookupKey = data.getString(1);
    
        val contactUri = Contacts.getLookupUri(contactID, lookupKey)
        if (contactUri == null){
            Log.i(TAG_LABEL, "contactUri je null, invalid arguments?")
            return "fail";
        }
        Log.i(TAG_LABEL, "contact uri: " + contactUri.toString()) // valid and expected Uri

        // Get the path of the ringtone you'd like to use
        val storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path
        Log.i(TAG_LABEL, "out: $storage")
        
        val file = File(storage, "ringtone.mp3")
        Log.i(TAG_LABEL, file.exists().toString()) // reasures me that the file exists
        
        val value = Uri.fromFile(file).toString()
        
        Log.i(TAG_LABEL, "size: " + file.length().toString() + " name: " + file.name)

        val values = ContentValues()
        values.put(Contacts.CUSTOM_RINGTONE, value)

        // TRIED A COMBINATION OF THESE, THEY DON'T WORK
        // values.put(MediaStore.MediaColumns.TITLE, file.nameWithoutExtension);
        // values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
        // values.put(MediaStore.MediaColumns.SIZE, file.length())
        // 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);
        // values.put(MediaStore.Audio.Media.ARTIST, "example")

        val res = contentResolver.update(contactUri, values, null, null)
        Log.i(TAG_LABEL, "content resolver res: " + res.toString())

    }finally {
        data.close();
    }

    return "succes"
}

Once again, it runs fine on Android 9 and below. Also, i can't seem to find any information in the official documentation that mentions changes in Contacts services or similar.

Waldi
  • 39,242
  • 6
  • 30
  • 78
Shanko
  • 9
  • 3
  • Your try block has no catch block. – blackapps Jan 12 '21 at 13:47
  • Try a file from getExternalFilesDir(). – blackapps Jan 12 '21 at 13:50
  • Android 10 changes the way to access files in external storage. You cannot still use path to access file. To use file you need his `uri`. Let read about [scoped storage](https://developer.android.com/about/versions/11/privacy/storage). In android 10 is one way to skip this (set `requestLegacyExternalStorage` flag to true), but in android 11 this won't work anymore. – grabarz121 Jan 12 '21 at 15:21
  • I know i don’t have a catch block but it logs the last message, i doesn’t throw an exception :( – Shanko Jan 12 '21 at 15:26

0 Answers0