0

If i am trying to assign ringtone that is located in my sd card to a particular contact. It's working perfectly but the problem is that that ringtone is assigned to all of the contacts, not to particular contact.

Here is my code :-

((ListView) v.findViewById(R.id.contact_list)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ContactModel contactModel = (ContactModel) adapterView.getItemAtPosition(i);

                AudioModel audioModel = (AudioModel) getArguments().getSerializable("AudioModel");

                ContentValues values = new ContentValues();
                String username = contactModel.getName();

                Log.e("SYNC", "setting ringtone for " + username);
                ContentResolver resolver = getActivity().getContentResolver();

                File root = Environment.getExternalStorageDirectory();

                Log.e("test", "ringtone checkpoint name here: beautiful ");

                File file = new File(audioModel.getPath());
                if(file.exists()) {

                    Log.e("test", "ringtone checkpoint if file exists");

                    Uri oldUri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
                    resolver.delete(oldUri, MediaStore.MediaColumns.DATA + "=\"" + file.getAbsolutePath() + "\"", null);

                    Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactModel.getContact_id());

                    values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
                    values.put(MediaStore.MediaColumns.TITLE, "Beautiful");
                    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
                    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

                    Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
                    Uri newUri = resolver.insert(uri, values);
                    String uriString = newUri.toString();
                    values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, uriString);
                    Log.e("Uri String for " + ContactsContract.Contacts.CONTENT_URI, uriString);
                    long updated = resolver.update(contactUri, values,null, null);
                    Toast.makeText(getActivity(), "Updated : " + updated, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getActivity(), "File does not exist", Toast.LENGTH_LONG).show();
                }

            }
        });

If I am using this code, it's working but ringtone is assigned to all contacts not to one.

resolver.update(ContactsContract.Contacts.CONTENT_URI, values,null, null);

But When i am using this code, it's not working.

Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactModel.getContact_id());
resolver.update(contactUri, values,null, null);
asdf
  • 11
  • 3

2 Answers2

0

You don't have any where clause on your update, so it will update all contacts. That's what the two final parameters are- the where clause and an array of values for it.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

According to this question i implemented how to set ringtone for a contact and i posted answer here

Community
  • 1
  • 1
Bahu
  • 1,516
  • 2
  • 28
  • 49