Custom Contact Ringtone Not Working on Some devices

你说的曾经没有我的故事 提交于 2020-01-21 10:15:10

问题


I'm developing music player, in that i have to set audio file as ringtone for contacts. Below code is working for some phones and some phones not.

    public static int setContactRingtone(@NonNull Context context, @NonNull Song song, String number) {
        int value = 0;
        if (context == null) {
            value = 0;
        }
        File f = new File(song.getmSongPath());
        final Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number);
        final String[] projection = new String[] {
                ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
        };
        final Cursor data = context.getContentResolver().query(lookupUri, projection, null, null, null);
        if (data != null && data.moveToFirst()) {
            final long contactId = data.getLong(0);
            final String lookupKey = data.getString(1);
            final Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
            if (contactUri != null) {
                final String valuex = Uri.fromFile(f).toString();
                final ContentValues values = new ContentValues(1);
                values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, valuex);
                if (contactUri != null) {
                    int ooo = context.getContentResolver().update(contactUri, values, null, null);
                    if (ooo > 0){
                        value = 1;
                    }
                }
            }
            data.close();
        }
        return value;
    }

来源:https://stackoverflow.com/questions/59020167/custom-contact-ringtone-not-working-on-some-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!