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

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

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. getContentResolver().delete(uri, null, null);  
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 调用方法:  
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. setUserCustomVoice(outPathRingFile,RingtoneManager.TYPE_ALARM);  

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. private void setUserCustomVoice (String path, int type)  
  2.     {         
  3.   
  4.         /*Sometimes, this function will be failed because of the wrong path.  
  5.          * Only the path "/sdcard/media/audio/ringtones/" is available to set ring.  
  6.          * For example:  
  7.          * setUserCustomVoice("/sdcard/media/audio/ringtones/wang.mp3",RingtoneManager.TYPE_ALARM);  
  8.          */  
  9.         File sdfile = new File(path);      
  10.         String mimeType = "audio/*";  
  11.         ContentValues values = new ContentValues();    
  12.         values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());    
  13.         values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());    
  14.         values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);  
  15.         values.put(MediaStore.MediaColumns.SIZE, sdfile.length());    
  16.           
  17.   
  18.         values.put(MediaStore.Audio.Media.IS_RINGTONE, false);    
  19.         values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);    
  20.         values.put(MediaStore.Audio.Media.IS_ALARM, false);    
  21.         values.put(MediaStore.Audio.Media.IS_MUSIC, false);    
  22.   
  23.         switch (type)  
  24.         {  
  25.         case RingtoneManager.TYPE_NOTIFICATION:  
  26.             values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);  
  27.             break;  
  28.         case RingtoneManager.TYPE_ALARM:  
  29.             values.put(MediaStore.Audio.Media.IS_ALARM, true);  
  30.             break;  
  31.         case RingtoneManager.TYPE_RINGTONE:  
  32.             values.put(MediaStore.Audio.Media.IS_RINGTONE, true);  
  33.             break;  
  34.         default:  
  35.             type = RingtoneManager.TYPE_ALL;  
  36.             values.put(MediaStore.Audio.Media.IS_MUSIC, true);    
  37.             break;    
  38.         }  
  39.           
  40.         System.out.println("values: " + values);  
  41.           
  42.         System.out.println("Ring absPath: " + sdfile.getAbsolutePath());  
  43.         System.out.println("RingPath: " + path);  
  44.           
  45.         Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());  
  46.         System.out.println("uri: " + uri);  
  47.           
  48.         getContentResolver().delete(uri, null, null);  
  49.         /*The following code always returns null, Why? Why?*/  
  50.         final Uri newUri = getContentResolver().insert(uri, values);  
  51.           
  52.         //getContentResolver().update(uri, values, null, null);  
  53.         System.out.println("newUri: "+newUri);  
  54.         RingtoneManager.setActualDefaultRingtoneUri(RingToneTailor.this, type, newUri);    
  55.     }  



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


//------------------------------------------------------------------------------------

总结:这里是反编译看到源码里面如此写,所以产生了疑问。

ContentValues localContentValues = new ContentValues();
      localContentValues.put("_data", paramFile.getAbsolutePath());
      localContentValues.put("title", paramFile.getName());
      localContentValues.put("mime_type", "audio/*");
      localContentValues.put("is_ringtone", Boolean.valueOf(bool4));
      localContentValues.put("is_notification", Boolean.valueOf(bool3));
      localContentValues.put("is_alarm", Boolean.valueOf(bool2));
      localContentValues.put("is_music", Boolean.valueOf(bool1));
      Uri localUri = MediaStore.Audio.Media.getContentUriForPath(paramFile.getAbsolutePath());
      getContentResolver().delete(localUri, "_data=\"" + paramFile.getAbsolutePath() + "\"", null);
      RingtoneManager.setActualDefaultRingtoneUri(this, paramInt, getContentResolver().insert(localUri, localContentValues));
这里调用了getContentResolver().delete();方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值