getcontentresolver()。insert始终在android 4.4及更高版本的设备中返回null

时间:2015-03-31 09:57:22

标签: android android-contentprovider android-4.4-kitkat android-contentresolver ringtone

iam尝试使用以下代码以编程方式设置铃声。它在果冻豆设备中工作正常,但它在Kitkat设备中不起作用。我总是将newUri视为null。

    private void SetAsRingtone(String filepath) {
    // TODO Auto-generated method stub

    File ringtoneFile = new File(filepath);
    ContentValues content = new ContentValues();
    content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
    content.put(MediaStore.MediaColumns.TITLE, "test");
    content.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length());
    content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    content.put(MediaStore.Audio.Media.ARTIST, "artist");
    content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    content.put(MediaStore.Audio.Media.IS_ALARM, false);
    content.put(MediaStore.Audio.Media.IS_MUSIC, false);
    Log.i("RingtonesAdapter", "the absolute path of the file is :"+ringtoneFile.getAbsolutePath());
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile.getAbsolutePath());
      context.getContentResolver().delete(
               uri,
               MediaStore.MediaColumns.DATA + "=\""
                       + ringtoneFile.getAbsolutePath() + "\"", null);
    Uri newUri = context.getContentResolver().insert(uri, content);

    try{
    RingtoneManager.setActualDefaultRingtoneUri(context,RingtoneManager.TYPE_RINGTONE,newUri);
    Settings.System.putString(context.getContentResolver(),
            Settings.System.RINGTONE, newUri.toString());       

       } catch (Throwable t) {

       }
}    

下面是我的文件路径和uri

filepath:/storage/emulated/0/audiofiles/MUSIC_19.mp3 Uri:content:// media / external / audio / media

我还在清单

中包含了“WRITE_SETTINGS”权限

请有人让我知道我在哪里做错了。我是否需要添加任何其他权限?

0 个答案:

没有答案