Saving file as a ringtone, function always returns false, no errors
I'm doing an app which as one part features a soundboard. I'm implementing a neatly written function 'saveas();' of which I'm not the author, however it always returns False. I've spent 2 days finding a solution and now I'm out of ideas and could use some help. The file never gets saved, nor the directory is being created. Thanks for any input.
// Save as Ringtone
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) {
// TODO Auto-generated catch block
return false;
}
String path="/sdcard/media/audio/ringtones/";
String filename="bio_ring"+".mp3";
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs()开发者_C百科;}
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
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, "Bio_ring");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "Bio_Ring");
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);
//Insert it into the database
Uri newUri= this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
return true;
}
From android2.2 the sdcard path is changed to "/mnt/sdcard". The best way is to get sdcard path using "Environment.getExternalStorage()"
String path= Environment.getExternalStorage() + "/media/audio/ringtones/";
This maybe one reason. You can add logs and find the reason from LogCat.
精彩评论