0

I'm using the following code to set the ringtone for individual contacts

String contact_number = "MOBILE_NUMBER";
        Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, contact_number);

        // The columns used for `Contacts.getLookupUri`
        String[] projection = new String[]{
                ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
        };

        // Build your Cursor
        Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);

        if (data != null && data.moveToFirst()) {
            data.moveToFirst();
            try {
                // Get the contact lookup Uri
                long contactId = data.getLong(0);
                String lookupKey = data.getString(1);
                Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
                if (contactUri == null) {
                    // Invalid arguments
                    return;
                }

                String ringtoneUri = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Test";
                File filePath = new File(ringtoneUri, "Bhar.MP3");

                Log.e("filePath", filePath.toString());

                String value = Uri.fromFile(filePath).toString();

                // Apply the custom ringtone
                ContentValues values = new ContentValues();
                values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
                values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, value);
                getContentResolver().update(contactUri, values, null, null);


            } finally {
                // Don't forget to close your Cursor
                data.close();
            }
        }

this code was working in most of the devices but it's not working in Samsung j5 (Android version 6.0.1) and i also checked in the same version contained devices (like Moto G3), in those devices it's working fine.

Note:

  1. I gave the all permissions related Marshmallow

  2. When i'm assigning the ringtone to J5 mobile. It's assigning the ringtone name to the particular contact but it's not playing the assigned ringtone. For understanding check the attached image

enter image description here

Bahu
  • 1,516
  • 2
  • 28
  • 49

1 Answers1

0

Following this question I tried to check the media URI instead of the file URI. Seems like some (or even most) devices, don't like setting the actual file without creating a ringtone media entry for the file first.

Here is my "ringtone download" code that works on all, followed by the code that does not work on some:

URLConnection cn = new URL(mediaUrl).openConnection();

        cn.connect();
        InputStream stream = cn.getInputStream();

        final File dir = new File(Environment.getExternalStorageDirectory().toString() + donloadedFilesDir);
        dir.mkdirs(); //create folders where write files
        final File file = new File(dir, getFileName(mediaUrl));

        FileOutputStream out = new FileOutputStream(file);
        byte buffer[] = new byte[1024];
        int dataSize;
        while ((dataSize = stream.read(buffer)) != -1) {
            out.write(buffer, 0, dataSize);
        }

        stream.close();
        out.flush();
        out.close();
        //Getting the file again to have the size after write
        File k = new File(dir, getFileName(mediaUrl));

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, "vCallRingtone");
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/" + getFileExtention(mediaUrl));
        values.put(MediaStore.MediaColumns.SIZE, k.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, false);

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

        return newUri.toString();

And here is the old code that did not work:

URLConnection cn = new URL(mediaUrl).openConnection();

        cn.connect();
        InputStream stream = cn.getInputStream();

        final File dir = new File(Environment.getExternalStorageDirectory().toString() + donloadedFilesDir);
        dir.mkdirs(); //create folders where write files
        final File file = new File(dir, getFileName(mediaUrl));

        FileOutputStream out = new FileOutputStream(file);
        byte buffer[] = new byte[1024];
        int dataSize;
        while ((dataSize = stream.read(buffer)) != -1) {
            out.write(buffer, 0, dataSize);
        }

        stream.close();
        out.flush();
        out.close();

        return Uri.fromFile(file).toString();
Community
  • 1
  • 1