歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 設置鈴聲,getContentResolver().insert returns null

Android 設置鈴聲,getContentResolver().insert returns null

日期:2017/3/1 9:48:35   编辑:Linux編程

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

Copyright © Linux教程網 All Rights Reserved