android 自定义铃声设置,在Android中设置铃声

android 自定义铃声设置,在Android中设置铃声

解决方案是在将资源文件资产提供给内容解析器进行插入之前,先获取资源文件资产并将其写入sdcard 1st。

File newSoundFile = new File("/sdcard/media/ringtone", "myringtone.oog");

Uri mUri = Uri.parse("android.resource://com.your.package/R.raw.your_resource_id");

ContentResolver mCr = app.getContentResolver();

AssetFileDescriptor soundFile;

try {

soundFile= mCr.openAssetFileDescriptor(mUri, "r");

} catch (FileNotFoundException e) {

soundFile=null;

}

try {

byte[] readData = new byte[1024];

FileInputStream fis = soundFile.createInputStream();

FileOutputStream fos = new FileOutputStream(newSoundFile);

int i = fis.read(readData);

while (i != -1) {

fos.write(readData, 0, i);

i = fis.read(readData);

}

fos.close();

} catch (IOException io) {

}

然后,您可以使用以前发布的解决方案

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/oog");

values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());

values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);

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

Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());

Uri newUri = mCr.insert(uri, values);

try {

RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);

} catch (Throwable t) {

Log.d(TAG, "catch exception");

}

别忘了写权限

在你的清单上

希望这可以帮助

发布者:admin,转转请注明出处:http://www.yc00.com/news/1703856044a1320824.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信