设为首页 加入收藏

TOP

Android 设置铃声,getContentResolver().insert returns null
2014-11-24 01:37:47 来源: 作者: 【 】 浏览:1
Tags:Android 设置 铃声 getContentResolver .insert returns null

下面插入铃声的代码,在调用下面代码的时候会出现返回null。而且没有任何报错信息。查了很久都无法找到原因。


final Uri newUri = getContentResolver().insert(uri, values);


然后改用update尝试,出现错误信息,错误提示是 "_data is not unique".


getContentResolver().update(uri, values, null, null);


分析原因可能是以前有这个column 记录不唯一。


所以在


final Uri newUri = getContentResolver().insert(uri, values);


前加上删除记录,问题解决。


getContentResolver().delete(uri, null, null);


调用方法:


setUserCustomVoice(outPathRingFile,RingtoneManager.TYPE_ALARM);


private void setUserCustomVoice (String path, int type)
{


/*Sometimes, this function will be failed because of the wrong path.
* Only the path "/sdcard/media/audio/ringtones/" is available to set ring.
* For example:
* setUserCustomVoice("/sdcard/media/audio/ringtones/wang.mp3",RingtoneManager.TYPE_ALARM);
*/
File sdfile = new File(path);
String mimeType = "audio/*";
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
values.put(MediaStore.MediaColumns.SIZE, sdfile.length());


values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);


switch (type)
{
case RingtoneManager.TYPE_NOTIFICATION:
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
break;
case RingtoneManager.TYPE_ALARM:
values.put(MediaStore.Audio.Media.IS_ALARM, true);
break;
case RingtoneManager.TYPE_RINGTONE:
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
break;
default:
type = RingtoneManager.TYPE_ALL;
values.put(MediaStore.Audio.Media.IS_MUSIC, true);
break;
}

System.out.println("values: " + values);

System.out.println("Ring absPath: " + sdfile.getAbsolutePath());
System.out.println("RingPath: " + path);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());
System.out.println("uri: " + uri);

getContentResolver().delete(uri, null, null);
/*The following code always returns null, Why Why */
final Uri newUri = getContentResolver().insert(uri, values);

//getContentResolver().update(uri, values, null, null);
System.out.println("newUri: "+newUri);
RingtoneManager.setActualDefaultRingtoneUri(RingToneTailor.this, type, newUri);
}


final Uri newUri = getContentResolver().insert(uri, values);


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇在Android中使用Lua脚本 —— Pro.. 下一篇Android 超炫的悬浮窗设计与实现

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: