我使用以下代码为各个联系人设置铃声
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();
}
}
此代码适用于大多数设备,但它在Samsung j5(Android版本6.0.1)中无效,我也检查了相同版本的设备(如Moto G3),在这些设备中它& #39;工作正常。
注意:
答案 0 :(得分:0)
关注this question我尝试检查媒体URI而不是文件URI。看起来像某些(甚至大多数)设备,不喜欢设置实际文件而不先为文件创建铃声媒体条目。
这是我的“铃声下载”代码,适用于所有代码,后面是对某些代码无效的代码:
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();
以下是旧代码无效:
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();