你好,游客 登录 注册 搜索
背景:
阅读新闻

Android 设置铃声,getContentResolver().insert returns null

[日期:2014-02-11] 来源:Linux社区  作者:yuxiaohui [字体:大 中 小]

在通过Android设置手机铃声的时候,在4.4版本总会遇到getContentResolver().insert  返回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相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android设置铃声  Android设置 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款