Search code examples
androidcontactsringtone

Set as Contact Ringtone? Android


I am trying to learn how to add set as contact ringtone feature. I already know how to set default ringtone but I can't figure how to set as contact ringtone. I got to the part where I choose contact, but I don't know how to assign ringtone to that contact. That part is bugging me and I can't seem to find answer in questions that were already asked on this topic. Here is my code so far:

static public final int CONTACT_CHOOSER_ACTIVITY_CODE = 73729;
private File csound;
private final File rpath = new File(Environment.getExternalStorageDirectory() + "/Ringtone sounds/Ringtones");


    @Override
    public void onClick(View v) {
        setContRing();

    }

    private void setContRing() {
        Boolean success = false;
        csound = new File(rpath, FNAME);rpath.mkdirs();
        if (!csound.exists()) {




            try {
                InputStream in = getResources().openRawResource(FPATH);
                FileOutputStream out = new FileOutputStream(csound.getPath());
                byte[] buff = new byte[1024];
                int read = 0;

                try {
                    while ((read = in.read(buff)) > 0) {
                        out.write(buff, 0, read);
                    }
                } finally {
                    in.close();

                    out.close();
                }
            } catch (Exception e) {
                success = false;

            }
        } else {
            success = true;
            setContRingtone();

        }

        if (!success) { 
           setContRingtone();


        }
    }

    private void setContRingtone() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        startActivityForResult(intent, CONTACT_CHOOSER_ACTIVITY_CODE);

    }




});


}

Edit for bounty: I am wondering if someone can show me how to do so, I tried with codes found in other questions but I couldn't apply them to my code. I can copy file but how to get contact and assign ringtone to that contact?


Solution

  • From set custom ringtone to specific contact number

    Android has a special column for this: ContactsContract.CUSTOM_RINGTONE.

    So, you could use ContactsContract.Contacts.getLookupUri to get your contact's Uri, after that pretty much all that's left is to call ContentResolver.update.

    Here's an example of looking up a contact by their phone number, then applying a custom ringtone:

    import android.provider.ContactsContract.Contacts;
    import android.provider.ContactsContract.PhoneLookup;
    
    // The Uri used to look up a contact by phone number
    final Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "012-345-6789");
    // The columns used for `Contacts.getLookupUri`
    final String[] projection = new String[] {
        Contacts._ID, Contacts.LOOKUP_KEY
    };
    // Build your Cursor
    final Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);
    data.moveToFirst();
    try {
        // Get the contact lookup Uri
        final long contactId = data.getLong(0);
        final String lookupKey = data.getString(1);
        final Uri contactUri = Contacts.getLookupUri(contactId, lookupKey);
        if (contactUri == null) {
            // Invalid arguments
            return;
        }
    
        // Get the path of ringtone you'd like to use
        final String storage = Environment.getExternalStorageDirectory().getPath();
        final File file = new File(storage + "/AudioRecorder", "hello.mp4");
        final String value = Uri.fromFile(file).toString();
    
        // Apply the custom ringtone
        final ContentValues values = new ContentValues(1);
        values.put(Contacts.CUSTOM_RINGTONE, value);
        getContentResolver().update(contactUri, values, null, null);
    } finally {
        // Don't forget to close your Cursor
        data.close();
    }
    

    Also, you'll need to add both permissions to read and write contacts:

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    

    To extend a bit on this, and how to modify it to your need, change phone number 012-345-6789 in this line to the one you are looking for

    // The Uri used to look up a contact by phone number
    final Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "012-345-6789");
    

    And set your default CUSTOM_RINGTONE in your phone ContactsContract. There is another, similar, option here: Setting contact custom ringtone, how?