I have two questions:
When I call my
saveas(ressound);
(inspired by stealthcopter.com) on a android 4.0.3 device the ringtone is nicely activated as a ringtone, but on a 2.3.3 device the selected ringtone is silent. Where did I go wrong in my code?The second thing that I want to know is how to delete the file. More importantly, how to get it out of the media store, does this happen automatically if I remove it from the sd?
UPDATE:
Added code to delete the whole folder plus content, works good and clean! (see below)
Try to call this code when I push a button:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
But it will not clean out the mediastore, it works with a reboot. I also tried the media scan within the dev tools, but that doesn't work. They mention that some apps need to restart, but how to restart the service responsible for displaying the list of available ringtones without rebooting the whole device?
variables:
String soundname;
int ressound;
int savetype;
String path="/sdcard/mysounds/";
savetype = RingtoneManager.TYPE_RINGTONE;
ressound = R.raw.sound1;
soundname = "sound1";
saveas(ressound);
//save as
public boolean saveas(int ressound){
byte[] buffer=null;
InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
int size=0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
return false;
}
String filename= soundname +".mp3";
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_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, soundname);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Elvis");
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);
//Insert it into the database
Uri newUri = this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
// set as ringtone
RingtoneManager.setActualDefaultRingtoneUri(this, savetype, newUri);
return true;
}
delete();
public void delete() {
File a = new File(path, "sound1.mp3");
a.delete();
File b = new File(path, "sound2.mp3");
b.delete();
}
deleteFiles(String path);
public static void deleteFiles(String path) {
File file = new File(path);
if (file.exists()) {
String deleteCmd = "rm -r " + path;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) { }
}
}