I got this code: `
protected void setRingtone() {
// TODO Auto-generated method stub
Boolean success = false;
sound = new File(folder, "Ilidan.mp3");
if (!sound.exists()) {
Log.i(TAG, "Ilidan " + folder.toString());
try {
InputStream in = getResources().openRawResource(R.raw.ilidan);
FileOutputStream out = new FileOutputStream(sound.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;
Log.i(TAG, "Ne da radi.");
}
} else {
success = true;
Log.i(TAG, "VEc postoji.");
}
if (!success) {
onSetRingtoneError("There was issue writting file.");
} else {
setSettingsRingtone();
}
}
private void onSetRingtoneError(String string) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Nije Radilo ", 5000).show();
}
private void setSettingsRingtone() {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, sound.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Alert");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
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, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(sound.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + sound.getAbsolutePath() + "\"",
null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this, RingtoneManager.TYPE_RINGTONE,
newUri);
}
{
}
But here is what happens. When I click on button first time, it creates file only, second time when I click it sets as ringtone. What am I doing wrong? How can I make my app to copy file and set it as ringtone in 1 click?
protected void setRingtone() {
// TODO Auto-generated method stub
Boolean success = false;
sound = new File(folder, "Ilidan.mp3");
if (!sound.exists()) {
Log.i(TAG, "Ilidan " + folder.toString());
try {
InputStream in = getResources().openRawResource(R.raw.ilidan);
FileOutputStream out = new FileOutputStream(sound.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();
success = true;
}
} catch (Exception e) {
success = false;
Log.i(TAG, "Ne da radi.");
}
} else {
success = true;
Log.i(TAG, "VEc postoji.");
}
if (!success) {
onSetRingtoneError("There was issue writting file.");
} else {
setSettingsRingtone();
}
}
private void onSetRingtoneError(String string) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Nije Radilo ", 5000).show();
}
private void setSettingsRingtone() {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, sound.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Alert");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
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, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(sound.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + sound.getAbsolutePath() + "\"",
null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this, RingtoneManager.TYPE_RINGTONE,
newUri);
}
{
}
i just change your finally like
finally {
in.close();
out.close();
success = true;
}
coz first time only create folder and success boolean is false then next time it will be true so just update your finally