在Android中以编程方式设置为原始文件夹中的默认铃声 [英] set as default ringtone from raw folder programmatically in android
本文介绍了在Android中以编程方式设置为原始文件夹中的默认铃声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过了这段代码,可以很好地将声音文件加载到铃声目录中.我可以从弹出窗口中手动选择声音.但是无法通过编程将其设置为默认铃声.plz帮助我将声音设置为以编程方式设置默认铃声
setContentView(R.layout.activity_main);
String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
path=(exStoragePath +"/media/alarms/");
// saveas();
saveas(RingtoneManager.TYPE_RINGTONE);
}
// In this method, we need to copy the mp3 file to the sd card location from
// where android picks up ringtone files
// After copying, we make the mp3 as current ringtone
public boolean saveas(int type) {
enter code here
byte[] buffer = null;
InputStream fIn = getBaseContext().getResources().openRawResource(
R.raw.sound);
int size = 0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
return false;
}
String filename = "sound";
boolean exists = (new File(path)).exists();
if (!exists) {
new File(path).mkdirs();
}
FileOutputStream save;
try {
save = new FileOutputStream(path + filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path + filename + ".mp3"
+ Environment.getExternalStorageDirectory())));
enter code here
File k = new File(path, filename);
ContentValues values = new ContentValues();
long current = System.currentTimeMillis();
values.put(MediaStore.MediaColumns.DATA, path + filename );
values.put(MediaStore.MediaColumns.TITLE, filename );
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp3");
//new
values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
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);
RingtoneManager.getRingtone(context,this.getContentResolver()
.insert(MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath()), values));
return true;
}
推荐答案
删除此代码
RingtoneManager.getRingtone(context,this.getContentResolver()
.insert(MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath()), values));
Remove this code
RingtoneManager.getRingtone(context,this.getContentResolver()
.insert(MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath()), values));
并将这些行代码放在返回true及其工作之前
and put these lines code before return true and its work
Uri newUri = this.getContentResolver()
.insert(MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath()), values);
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
这篇关于在Android中以编程方式设置为原始文件夹中的默认铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文