ContentResolver.insert总是返回null [英] ContentResolver.insert always returning null
问题描述
我试图通过设置$ P $自定义铃声pssing一个按钮,但是从ContentResolver的的插入方法越来越空。下面是设置的铃声code resopnsible。我查了计算器本身和一些用户称,其为他们工作,但我的情况下,我得到的变量newUri为空
声音文件被在所需位置创建,所以我知道newSoundFile就行了。
在打印时值,在日志中的变量给出了如下。
值是标题=我的手机铃声is_notification =真MIME_TYPE =音频/ MP3 is_alarm =真is_ringtone =真_size = 8733 is_music =假艺术家=无_data =到/ mnt / SD卡/媒体/铃声/ myringtone.mp3
尝试
{
的uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
Log.d(测试,uri.toString());
ContentValues值=新ContentValues();
values.put(MediaStore.MediaColumns.DATA,newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE,我的手机铃声);
values.put(MediaStore.MediaColumns.MIME_TYPE,音频/ MP3);
values.put(MediaStore.MediaColumns.SIZE,newSoundFile.length());
values.put(MediaStore.Audio.Media.ARTIST,无);
values.put(MediaStore.Audio.Media.IS_RINGTONE,真);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION,真);
values.put(MediaStore.Audio.Media.IS_ALARM,真);
values.put(MediaStore.Audio.Media.IS_MUSIC,FALSE);
Log.d(测试,价值观是+ values.toString());
newUri = mCr.insert(URI,价值观);
}
赶上(例外EE)
{
Log.d(测试,异常是+ EE);
}
如果(newUri == NULL)
Log.d(测试,newUri为空);
其他
Log.d(测试,newUri为+ newUri.toString());
使用此code。
ContentValues值=新ContentValues();
values.put(MediaStore.MediaColumns.DATA,newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE,我的手机铃声);
values.put(MediaStore.MediaColumns.MIME_TYPE,音频/ MP3);
values.put(MediaStore.Audio.Media.ARTIST,无);
values.put(MediaStore.MediaColumns.SIZE,215454);
values.put(MediaStore.Audio.Media.IS_RINGTONE,真);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION,FALSE);
values.put(MediaStore.Audio.Media.IS_ALARM,FALSE);
values.put(MediaStore.Audio.Media.IS_MUSIC,FALSE); URI URI = MediaStore.Audio.Media.getContentUriForPath(newSoundFile
.getAbsolutePath());
getContentResolver()。删除(
URI,
MediaStore.MediaColumns.DATA += \\
+ newSoundFile.getAbsolutePath()+\\,NULL);
乌里newUri = getContentResolver()插入(URI,价值观)。 RingtoneManager.setActualDefaultRingtoneUri(
RingtonesPlaying.this,RingtoneManager.TYPE_RINGTONE,
newUri);
I am trying to set a custom ringtone by pressing a button but getting null from ContentResolver's insert method. Following is the code resopnsible for setting the ringtone. I checked on stackoverflow itself and some users said its working for them but in my case I get the variable "newUri" as null
The sound file gets created at the desired location so I know that "newSoundFile" is ok. Upon printing "values" variable in log gives the following.
Values are title=my ringtone is_notification=true mime_type=audio/mp3 is_alarm=true is_ringtone=true _size=8733 is_music=false artist=None _data=/mnt/sdcard/media/ringtone/myringtone.mp3
try
{
uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
Log.d("Test", uri.toString());
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
values.put(MediaStore.Audio.Media.ARTIST, "None");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Log.d("Test","Values are "+values.toString());
newUri = mCr.insert(uri, values);
}
catch (Exception ee)
{
Log.d("Test", "Exception is " + ee);
}
if(newUri == null)
Log.d("Test", "newUri is Null");
else
Log.d("Test", "newUri is "+newUri.toString());
Use this code.
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "None");
values.put(MediaStore.MediaColumns.SIZE, 215454);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile
.getAbsolutePath());
getContentResolver().delete(
uri,
MediaStore.MediaColumns.DATA + "=\""
+ newSoundFile.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
RingtonesPlaying.this, RingtoneManager.TYPE_RINGTONE,
newUri);
这篇关于ContentResolver.insert总是返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!