无法设置为铃声,因为文件已存在 [英] Unable to set as Ringtone because file already exists
本文介绍了无法设置为铃声,因为文件已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将设置为铃声功能的应用制作为手机,但出现问题. 当我设置为铃声后,它就开始工作了.但是,当我第二次尝试设置为铃声时,什么也没发生. 现在的问题是因为文件已经存在.如何将代码设置为喜欢...如果文件已存在,请继续执行下一步 这是我的代码:
I am trying to make app with set as ringtone feature but I got a problem. When I Set as ringtone once it's working. But when I try to set as ringtone for second time, nothing happens. Now the problem is because file already exist. How can I set my code to like...If file already exist proceed to next step Here is my code:
private File rsound;
private final File rpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES);
private void s1ring() {
Boolean success = false;
rsound = new File(rpath, " Thesound.mp3");
if (!rsound.exists()) {
try {
InputStream in = getResources().openRawResource(R.raw.s1a64);
FileOutputStream out = new
FileOutputStream(rsound.getPath());
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
} catch (Exception e) {
success = false;
}
} else {
success = true;
}
if (!success) {
setRingtone();
}
}
private void setRingtone() {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, rsound.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Thesound");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.Audio.Media.ARTIST, "The ringtones");
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, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(rsound.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + rsound.getAbsolutePath() + "\"",
null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
S1.this, RingtoneManager.TYPE_RINGTONE,
newUri);
Toast.makeText(getApplicationContext(), "Ringtone set successfully",
Toast.LENGTH_SHORT).show();
}
}
);
推荐答案
像这样调整您的代码:
if (!rsound.exists()) {
//your create sound file code here.
} else {
//call set ringtone method also for the case file exists:
setRingtone();
}
这篇关于无法设置为铃声,因为文件已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文