0

As many people asked here ,I figured out the process of setting custom rings to specifc contacts by using the following link:

Setting contact custom ringtone, how?

I used @viperbone's solution and it helped me showing the user the contact picking window and getting the id of selected person.

Now the problem is: after all codes run ,the ringtone for contact chosen is still the same.

I searched many topic in here and googled it and they all do this process kind of using same method but mine just doesn't work.

here is the code:

public class SetCustomContacts extends Activity {                                                                                                              

    // put that constant in your class                                                                                                                         
    static public final int CONTACT_CHOOSER_ACTIVITY_CODE = 73729;                                                                                             
    private String musicId;                                                                                                                                    

    @Override                                                                                                                                                  
    protected void onCreate(Bundle savedInstanceState) {                                                                                                       
        requestWindowFeature(Window.FEATURE_NO_TITLE);                                                                                                         
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);                                     
        super.onCreate(savedInstanceState);                                                                                                                    

        musicId = getIntent().getExtras().getString("music_id");                                                                                               

        Intent intent = new Intent(Intent.ACTION_PICK);                                                                                                        
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);                                                                                                
        startActivityForResult(intent, CONTACT_CHOOSER_ACTIVITY_CODE);                                                                                         
    }                                                                                                                                                          
    @Override                                                                                                                                                  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {                                                                            
        super.onActivityResult(requestCode, resultCode, data);                                                                                                 
        switch (requestCode) {                                                                                                                                 
        case (CONTACT_CHOOSER_ACTIVITY_CODE) :                                                                                                                 
            if (resultCode == Activity.RESULT_OK) {                                                                                                            
                try{                                                                                                                                           
                    Scanner in = new Scanner(musicId).useDelimiter("[^0-9]+");                                                                                 
                    int integer = in.nextInt();                                                                                                                
                    String filePath2 = getFilesDir() + File.separator + String.valueOf(integer) + ".mp3";                                                      
                    File ringFile = new File(filePath2);                                                                                                       
                    File fileDest = null;                                                                                                                      
                    try{                                                                                                                                       
                        fileDest = new File(Environment.getExternalStorageDirectory().getPath(), "ringSpecificContact" + ".mp3");                          
                        copy(ringFile, fileDest);                                                                                                              
                    }catch (Exception ex){                                                                                                                     
                        Log.d("File Copy: ", ex.toString());                                                                                                   
                    }                                                                                                                                          

                    Uri contactData = data.getData();                                                                                                          
                    String contactId = contactData.getLastPathSegment();                                                                                       
                    String[] PROJECTION = new String[] {                                                                                                       
                            ContactsContract.Contacts._ID,                                                                                                     
                            ContactsContract.Contacts.DISPLAY_NAME,                                                                                            
                            ContactsContract.Contacts.HAS_PHONE_NUMBER,                                                                                        
                    };                                                                                                                                         
                    Cursor localCursor =  getContentResolver().query(contactData, PROJECTION, null, null, null);                                               
                    localCursor.moveToFirst();                                                                                                                 

                    String contactID = localCursor.getString(localCursor.getColumnIndexOrThrow("_id"));                                                        
                    String contactDisplayName = localCursor.getString(localCursor.getColumnIndexOrThrow("display_name"));                                      

                    Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactID);                                                     
                    localCursor.close();                                                                                                                       
                    ContentValues localContentValues = new ContentValues();                                                                                    

                    localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);                                                                                                                     
                    localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE, Environment.getExternalStorageDirectory() + "/ringSpecificContact.mp3"); 
                    getContentResolver().update(localUri, localContentValues, null, null);                                                                     

                    Toast.makeText(this, contactDisplayName, Toast.LENGTH_LONG).show();             

                } catch(Exception ex){                                                                                                                         
                    Log.d("on onActivityResult: ", ex.toString());                                                                                             
                }                                                                                                                                              
            }                                                                                                                                                  
            break;                                                                                                                                             
    }                                                                                                                                                          
         onBackPressed();                                                                                                                                      
    }                                                                                                                                                          
    public void copy(File src, File dst) throws IOException {                                                                                                  
        InputStream in = new FileInputStream(src);                                                                                                             
        OutputStream out = new FileOutputStream(dst);                                                                                                          

        // Transfer bytes from in to out                                                                                                                       
        byte[] buf = new byte[1024];                                                                                                                           
        int len;                                                                                                                                               
        while ((len = in.read(buf)) > 0) {                                                                                                                     
            out.write(buf, 0, len);                                                                                                                            
        }                                                                                                                                                      
        in.close();                                                                                                                                            
        out.close();                                                                                                                                           
    }                                                                                                                                                          
}                                                                                                                                  
Community
  • 1
  • 1
Nima Sakhtemani
  • 1,119
  • 2
  • 10
  • 21
  • nobody answers, what happened to this website? – Nima Sakhtemani Dec 09 '13 at 11:06
  • Hey Neema (I assume thats your real name), can you please tell us on which android version and which device you trying to apply this code to? As far as I remember that I used it on Android 4.0.2 maybe Google changed something if you try it on a newer version. – Bruno Bieri Dec 09 '13 at 17:30
  • I'm neema and the phone I'm using is samsung galaxy core ,running android 4.1.2. – Nima Sakhtemani Dec 10 '13 at 19:29
  • @neemasa, what had you done to assign the custom ringtone to a specific contact by picking it via intent, here is no answer and no further discussion, can you plz help me in this purpose. :) – Zia Ur Rahman Oct 03 '16 at 10:56

0 Answers0