2016-10-06 24 views
0

我用下面的代碼來設置單個聯繫人不進行分配的鈴聲在一些設備中

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(); 
      } 
     } 

這段代碼中的大部分設備是工作的鈴聲,但它不是在三星J5工作(Android版6.0.1),我也檢查了相同版本的設備(如Moto G3),在那些設備工作正常。

注:

  1. 我把所有權限相關的棉花糖

  2. 當我分配鈴聲J5移動。它將鈴聲名稱分配給特定的聯繫人,但它不播放指定的鈴聲。爲了瞭解檢查附加圖像

enter image description here

回答

0

this question我試圖檢查媒體URI而不是文件URI。看起來像一些(或甚至大多數)設備,不喜歡設置實際的文件,而不會創建鈴聲文件首先進入文件。

這裏是我的「鈴聲下載」的代碼,所有的工作,其次是代碼,不會對一些工作:

的URLConnection CN =新的URL(mediaUrl).openConnection();

這裏是舊代碼沒有工作:

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(); 
+0

謝謝您的解決方案 – Bahu

+0

@Bahu沒問題,你應該把它標記爲解決,如果沒有解決具體問題 –

+0

當然,我會接受,當我在工作 – Bahu