Android 将文件另存为铃声,函数始终返回false,无错误

Android 将文件另存为铃声,函数始终返回false,无错误,android,function,ringtone,Android,Function,Ringtone,我正在做一个应用程序,其中一个部分是一个音板。我正在实现一个写得很好的函数“saveas();”其中我不是作者,但它总是返回False。我花了两天时间寻找解决方案,现在我没有主意了,需要一些帮助。文件永远不会保存,也不会创建目录。谢谢你的意见 // Save as Ringtone public boolean saveas(int ressound){ byte[] buffer=null; InputStream fIn = getBaseContext().getResource

我正在做一个应用程序,其中一个部分是一个音板。我正在实现一个写得很好的函数“saveas();”其中我不是作者,但它总是返回False。我花了两天时间寻找解决方案,现在我没有主意了,需要一些帮助。文件永远不会保存,也不会创建目录。谢谢你的意见

// 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();}

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;            
}

SD卡路径从android2.2更改为“/mnt/sdcard”。最好的方法是使用“Environment.getExternalStorage()获取SD卡路径”

这也许是一个原因。
您可以添加日志并从LogCat中查找原因。

您是否声明使用:权限外部存储?
String path= Environment.getExternalStorage() + "/media/audio/ringtones/";