我正在使用下载管理器从服务器下载MP3文件。
这是它的代码。
public String createFilePath()
{
String path;
String dir = "APP_NAME";
path = Environment.getExternalStorageDirectory().getPath();
File file = new File(Environment.getExternalStorageDirectory(),dir);
if(!file.exists())
{
file.mkdir();
}
path += "/" +dir + "/";
System.out.println("-- saving path : " + path);
return path;
}
public void startDownload() {
Uri uri=Uri.parse(URLFixer.Fix(DATA.url));
System.out.println("-- download path : " + createFilePath() + FileNameGetter.getFileName(DATA..url));
DownloadManager.Request request = new Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle(DATA..title);
request.setDescription(DATA.artist + " - " + DATA.album);
request.setDestinationInExternalFilesDir(activity, createFilePath(), FileNameGetter.getFileName(DATA..url));
// request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
// request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
lastDownload= mgr.enqueue(request);
isDownloading = true;
Toasts.pop(activity, "Download Started!!");
// v.setEnabled(false);
// findViewById(R.id.query).setEnabled(true);
}
问题是,它应该保存在文件夹"APP_NAME"内的SD卡上,但一旦下载音频,我就看不到它在该文件夹内,当我播放音频并检查其信息时,它显示的路径类似于
/sdcard/Android/data/com.X.app/files/mnt/sdcard/APP_NAME/file.mp3
由于该文件保存在数据文件夹中,用户无法查看该文件。如何修复它以将其移动到主SD卡,即/mnt/sdcard/APP_NAME,以便用户可以看到它。
DownloadManager.Request.setDestinationUri(Uri uri)
应该符合您的要求,如果您希望MediaScanner扫描MP3,请记住调用allowScanningByMediaScanner()
。