Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Android 设置铃声,getContentResolver().insert returns null

在通过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