我正在尝试下载mp3文件,并希望将其保存到特定位置的内部存储。
但每次我的下载都失败了。
Logcat错误消息
01-01 08:36:09.295 154-748/android.process.media W/DownloadManager: Aborting request for download
17: Failed to create target file /storage/emulated/0/Ringtone/Fav_Ringtone.mp3
下载方法
public void Save_mp3_internal_Storage(Uri uri,Context context){
String destination = Environment.getExternalStorageDirectory()+"/Ringtone/";
Delete_mp3_internal_Storage(new File(destination,"Fav_Ringtone.mp3"));
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri)
.setDestinationUri(Uri.fromFile(new File(destination,"Fav_Ringtone.mp3")))
.setTitle("Ringtone Downloading")
.setNotificationVisibility(1);
manager.enqueue(request);
}
public void Delete_mp3_internal_Storage(File file){
if (file.exists()){
file.delete();
}
}
我认为这是模拟器的故障。因为大多数时候我的模拟器无法下载文件(SmartGaga(
将您的代码更改为此
public void Save_mp3_internal_Storage(Uri uri, Context context){
Delete_mp3_internal_Storage(context);
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalFilesDir(context,Environment.DIRECTORY_ALARMS,"Fav_Ringtone.mp3")
.setTitle("Ringtone Downloading")
.setNotificationVisibility(1);
manager.enqueue(request);
}
public void Delete_mp3_internal_Storage(Context context){
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_ALARMS),"Fav_Ringtone.mp3");
if (file.exists()){file.delete();}
}