I have used DownloadManager class for image download. When I have used below image url in browser it is working fine. but when i have downloaded that image url using DownloadManager it is getting .zip format.
Image Url : Here
Below is my code of download Manager :
private void startDownload(String url) {
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(true);
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
request.setMimeType(mimeString);
request.setTitle(getString(R.string.app_name));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Set a description of this download, to be displayed in notifications (if enabled)
request.setDescription("Downloading " + txtDocName.getText().toString());
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, System.currentTimeMillis() + ".jpeg");
downloadManager.enqueue(request);
AppLog.showD(TAG, "downloadind started");
}
The provided URL returns zip as a type. You can check that using any dev tool on your browser as demonstrated in this screen shot
Related
I am trying to download image from webview in custom folder of Android app. As of now i can only download file in Download folder by using below code.
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imgUrl));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
Is there any way to store image in other folder (newly created folder) instead of default Download folder. Actually i want to download image in Android app folder that will be private and not accessible to user via File Manager or other Apps.
I tried to create folder by using following code but folder is creating under /Android/data/com.abc.xyz/ is there any way to create folder directly under internal storage (under root directory) ?
File direct = new File(Environment.getExternalStorageDirectory()+"folder_name");
if(!direct.exists()) {
if(direct.mkdir());
}
try this
mywebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
File folder = new File("/storage/emulated/0/Folder_Name");
if (!folder.exists()) {
folder.mkdirs();
System.out.println("Directory created");
} else {
System.out.println("Directory is not created");
}
request.setDestinationInExternalPublicDir("Folder_Name","Wallpaper.jpg");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
I'm working with Android downloadmanager. I got this error when passing url to downloadmanager. It's said that Download unsuccessfully, But I tried to use my browser then paste url . It's response file mp3. I don't know why downloadmanager did not recognize file to download.
Here is example URL to test:
http://htstar.design/mp3zing.php?q=320&link=http://mp3.zing.vn/bai-hat/Tired-Alan-Walker-Gavin-James/ZW7FA7WA.html
Update Here is my method download:
private void downloadFile(String url, String name) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle(name.replace(ext, ""));
request.setMimeType("audio/MP3");
request.setDescription(name);
request.setDestinationInExternalPublicDir("/Music", name);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadManager.enqueue(request);
fabOpenDownload.setVisibility(View.VISIBLE);
}
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 start developing my very first Android project. In my project, I need to download media files, especially mp3 or mp4. I am downloading file using DownloadManager.
Here is my download code for mp3
private void downloadPodcast(int id)
{
String url = context.getResources().getString(R.string.api_endpoint)+"podcast/download?id="+String.valueOf(id);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading...");
request.setTitle("Podcast");
request.setMimeType("audio/MP3");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "audio.mp3");
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
As you can see in my code, I am downloading only mp3 and setting the MIME type is constant. The file name and its extension is constant as well. What I want to do is to detect the file extension I will download. So can I set the MIME type programmatically and set the extension of file name. How can I achieve it in DownloadManager?
Though its quite late, however here is the answer:
You can get the extension of the file to download using the code below and add the extension to your file name.
String fileUrl = "http://someurl";
String fileName = "foobar";
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
// concatinate above fileExtension to fileName
fileName += "." + fileExtension;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl))
.setTitle(context.getString(R.string.app_name))
.setDescription("Downloading " + fileName)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
In my application i have download(Image) feature which is used to download file from urls. The download happen should be shown in notification bar so that i used Download Manager class to download file. This is working fine but the downloaded image does not stored no where in the sdcard.
i have referred the url for the download manager.
my requirement is i need to save the download image to sdcard with notification bar indication. What to modify on the code to get save image on sdcard on the above link
i have some doubts regards the code in the above link is Can i use the same code to download audio or video file?
please help me.
Edited question:
I have tried
filepath = Environment.getExternalStorageDirectory().getPath()+"/download/cm.png";
Uri destinationUri = Uri.parse(filepath);
request.setDestinationUri(destinationUri);
before the preference manger on the button click. but i could not get the file on sdcard.
This is what i used.
Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setDescription("Downloading a file");
long id = downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("File Downloading...")
.setDescription("Image File Download")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "cm.png"));
In the code you refer to, the file is opened at the end. At this point, you can consider copying it to the SDCard.
Otherwise (better) use http://developer.android.com/reference/android/app/DownloadManager.Request.html setDestinationUri(android.net.Uri) to specify where you want to download the file.
downloadmanager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Toast.makeText(context, "Downloading...", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("---- url here ------");
request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle("---- title here ------");
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl("---- url here ------");
request.setMimeType(mimeType);
request.setDescription("---- descripation here ------");
if("---- titlehere ------" != null){
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "---- title here ------");
}
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadmanager.enqueue(request);