Hi i want to download video file and below is my code
public void file_download(String uRl) {
File direct = new File(Constant.FOLDER_PATH);
try {
uRl = "http://songs7.funmaza.in/videos/"
+ URLEncoder
.encode("Issey Kehte Hain Hip Hop 720p - Yo Yo Honey Singh [Funmaza.com].wmv",
"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("URL For Download == " + uRl);
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mdDownloadManager = (DownloadManager) ((Activity) context)
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(uRl));
request.setDescription("Downloading via Your app name..");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.fromFile(direct));
mdDownloadManager.enqueue(request);
}
and in download manager I got following information
If i pass simple URL like "http://beta-vidizmo.com/hilton.mp4" then it is working fine
Try this way,hope this will help you to solve your problem.
file_download("http://songs7.funmaza.in/videos/Issey Kehte Hain Hip Hop 720p - Yo Yo Honey Singh [Funmaza.com].wmv",context);
public void file_download(String url,Context context) {
url = url.replace(" ","%20");
DownloadManager downloadManager = (DownloadManager) ((Activity) context).getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Demo")
.setDescription("Downloading via Your app name..")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory() + "/MyFolder", "test1.3gp");
downloadManager.enqueue(request);
}
Finally Try and error, I found this solution,
File direct = new File(Constant.FOLDER_PATH);
uRl = "http://songs7.funmaza.in/videos/Issey Kehte Hain Hip Hop 720p - Yo Yo Honey Singh [Funmaza.com].wmv";
uRl = uRl.replace(" ", "%20");
uRl = uRl.replace("[", "%5B");
uRl = uRl.replace("]", "%5D");
System.out.println("URL For Download == " + uRl);
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager downloadManager = (DownloadManager) ((Activity) context)
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(uRl));
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setDescription("Downloading via Your app name..")
.setTitle("Issey Kehte Hain Hip Hop")
.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir("/MoviesAndSongs",
"test1.mp4");
downloadManager.enqueue(request);
It's done :) Thanks Haresh to you also
Related
i am trying to save a PDFDocument on my phone but i am not able too. this is my code;
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),"/" +nameGet1 + "-" + idGet1+".pdf");
Try using this code, and make use you have permission to save on phone
download.setOnClickListener(v -> {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(your_file_url));
request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(your_file_url));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, your_file_name);
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
});
I'm trying to download the file from the server using DownloadManager but It fail's to download and say's Download unsuccessful, I have read that there is a issue with Download Manager in the below link
android-chrome-browser-unnecessarily-renames-names-types-of-downloaded-files so I tried to download the file by adding the RequestHeader to the DownloadManagerRequest using below method but nothing happened. please find the below code part of my Downloader class anyone please suggest me the right way to download the file from url using DownloadManager.
url:
http://-storage..net/95-****/95-3194.pdf?AWSAccessKeyId=T9YG9HWQC1LHE5G5VF38&Expires=1483443248&Signature=Z%2BkWzfEI2VawbCx%2F2Yto1kPcJKA%3D
Thanks
public void download(Uri uri) {
if (!isDownloading()) {
String fileName=getName(uri);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(fileName);
request.setNotificationVisibility(1);
request.allowScanningByMediaScanner();
request.addRequestHeader("Content-Type", "application/octet-stream");
request.addRequestHeader("Content-Disposition", "attachment; filename=\""+fileName.split("\\.")[0]+"."+fileName.split("\\.")[1].toUpperCase()+"\"");
String cookieContent = getCookieFromAppCookieManager(uri.getHost());
request.addRequestHeader("Cookie", cookieContent);
downloadId = downloadManager.enqueue(request);
register();
}
}
and below block returns the File name
private String getName(Uri uri) {
String name=uri.toString().split("jabord/")[1].split("\\?")[0];
return name;
}
returns the appcookie manager
public String getCookieFromAppCookieManager(String url) {
android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
if (cookieManager == null) {
return null;
}
String rawCookieHeader = null;
// Extract Set-Cookie header value from Android app CookieManager for this URL
rawCookieHeader = cookieManager.getCookie(url);
if (rawCookieHeader == null) {
return null;
}
return rawCookieHeader;
};
Please remove the headers and set the mimetype and then it will work. For example
public void download(Uri uri) {
if (!isDownloading()) {
String fileName=getName(uri);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(fileName);
request.setNotificationVisibility(1);
request.allowScanningByMediaScanner();
request.setMimeType("application/pdf");
Log.e("Extension with ","UpperCase-->"+"\""+fileName.split("\\.")[0]+"."+fileName.split("\\.")[1].toUpperCase()+"\"");
downloadId = downloadManager.enqueue(request);
register();
}
}
I was also facing the same issue. I solved it by removing the cookies.
i'm trying to develop an app that shows video and you can download it on your mobile
the app is working perfectly, but my problem is that the download arrow doesn't appear in notification bar.
I'm using download manager class.
Here's my method:
public void downloadFileFromUrl(String url, String fileName, DownloadManager downloadManager) {
String filePath=Environment.getExternalStorageDirectory() + File.separator + "BlueNet";
try {
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setDestinationInExternalPublicDir("/BlueNet",fileName);
request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
request.allowScanningByMediaScanner();
idDwnldMng= downloadManager.enqueue(request);
}
catch (Exception ex){
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
Can someone help me?
Seems like you missing the permission:
<uses-permission
android:name="WRITE_EXTERNAL_STORAGE" />
I have this code that downloads an image but for some reason the image is in my downloads as "queued" instead of downloading.
String url = file.public_url;
getContext();
DownloadManager manager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(file.filename);
request.setDescription(file.filename + " description.");
request.allowScanningByMediaScanner();
request.setAllowedOverRoaming(true);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, file.filename + file.file_extension);
manager.enqueue(request);
I have successfully downloaded the images with this Async version that doesn't use DownloadManager https://stackoverflow.com/a/3028660/6136947
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);
}
There are two options available for you to use.
1) first setDestinationInExternalPublicDir this will let you download in any of the androids standard download folder based on media type eg DIRECTORY_DOWNLOADS, DIRECTORY_MUSIC. these files will remain after uninstall.
request.setDestinationInExternalPublicDir(DIRECTORY_DOWNLOADS,
File.separator + folderName + File.separator + fileName);
The first argument should be a standard downloads directory for this to work properly and cannot be anything else.
2) second is setDestinationInExternalFilesDir this is same as the previous method with the difference that these files will be deleted after app uninstall.
request.setDestinationInExternalFilesDir(context, DIRECTORY_DOWNLOADS,
File.separator + folderName + File.separator + fileName);
here the second argument can be null or any of the android download directories.
Try Below Code:.
String storagePath = Environment.getExternalStorageDirectory()
.getPath()
+ "/Directory_name/";
//Log.d("Strorgae in view",""+storagePath);
File f = new File(storagePath);
if (!f.exists()) {
f.mkdirs();
}
//storagePath.mkdirs();
String pathname = f.toString();
if (!f.exists()) {
f.mkdirs();
}
// Log.d("Storage ",""+pathname);
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri uri = Uri.parse(image);
checkImage(uri.getLastPathSegment());
if (!downloaded) {
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir("/Directory_name", uri.getLastPathSegment());
Long referese = dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading...", Toast.LENGTH_SHORT).show();
}
For Set Your Path For Download File Use: Work For me (Android 11).
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/YOUR FOLDER/", "YOUR FILE.(mp3|mp4|pdf|...)");
request.setDestinationUri(Uri.fromFile(file));
Complete Code:
First Check Directory
private boolean CreateDirectory() {
boolean ret = false;
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getPath() + "/YOUR FOLDER/");
if (!dir.exists()) {
try {
dir.mkdirs();
ret = true;
} catch (Exception e) {
ret = false;
e.printStackTrace();
}
}
return ret;
}
Then:
String URL = " YOUR URL ";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setTitle("YOUR TITLE");
request.setDescription("YOUR DESCRIPTION");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/YOUR FOLDER/", "YOUR FILE.(mp3|mp4|pdf|...)");
request.setDestinationUri(Uri.fromFile(file));
DownloadManager manager= (DownloadManager)
getSystemService(Context.DOWNLOAD_SERVICE);
Long downloadId= manager.enqueue(request);
ok,Finish