设置自定义文件夹Android的下载管理器 [英] Set custom folder Android Download Manager
问题描述
我有一个关于下载管理器的问题。 我会从网站上下载的文件。当我设置的默认目录下载(Environment.DIRECTORY_DOWNLOAD)一切工作正常,我的下载启动。但是,如果我试图改变的目录,我的应用程序不下载该文件。特别是,我希望我的文件,去到一个文件夹内的下载,例如/存储/ SD卡/下载/ MyFolder的。我该如何解决呢?
文件mydownload =新的文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+/ MyFolder的);
如果(!mydownload.exists()){
mydownload.mkdir();
}
字符串URL = SURL [0];
DownloadManager.Request请求=新DownloadManager.Request(Uri.parse(URL));
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(mydownload.getAbsolutePath(),Myfile.extension);
下载管理经理=(下载管理器)getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(要求);
检查以下code:在的保存文件SD卡/ dhaval_files /
。只需更换您的文件夹名称,并对在Android清单文件权限 WRITE_EXTERNAL_STORAGE
。
公共无效file_download(字符串URL){
文件直接=新的文件(Environment.getExternalStorageDirectory()
+/ dhaval_files);
如果(!direct.exists()){
direct.mkdirs();
}
下载管理经理=(下载管理器)this.getSystemService(Context.DOWNLOAD_SERVICE);
乌里downloadUri = Uri.parse(URL);
DownloadManager.Request请求=新DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(假).setTitle(演示)
.setDescription(一些有用的东西。不,真的。)
.setDestinationInExternalPublicDir(/ dhaval_files,test.jpg放在);
mgr.enqueue(要求);
}
I've a question about Download Manager. I'm going to download a file from a site. When I set the default directory for download (Environment.DIRECTORY_DOWNLOAD) all works fine and my download is started. But if I try to change the directory, my app doesn't download the file. In particular, I want my file to go into a folder inside a Download, for example /storage/sdcard/Download/myFolder. How can I fix that?
File mydownload = new File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+ "/myFolder");
if (!mydownload.exists()){
mydownload.mkdir();
}
String url = sUrl[0];
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(mydownload.getAbsolutePath(),"Myfile.extension");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
check below code: its save file in "sdcard/dhaval_files/"
. just replace your folder name and give permission write_external_storage
in android manifest file.
public void file_download(String uRl) {
File direct = new File(Environment.getExternalStorageDirectory()
+ "/dhaval_files");
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/dhaval_files", "test.jpg");
mgr.enqueue(request);
}
这篇关于设置自定义文件夹Android的下载管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!