how to open download manager by clicking? - android

how do i make it open the default download manager app by clicking on a file being downloaded from the statusbar? i want to handle the downloads in the same webvew, not in any other browser. this method handles it but i am not able to click the downloading files-
// Download manager
webView.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);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
also it downloads files without it's original name . every file is called "download" i want to fix this

Related

Downloaded APK files in android WebView not running

We have android app which is basically just webView.
We need to download APK files of this app (in case that the user needs an update).
Downloaded APK in chrome works. But if i download it in webView using following code, it is not running. You can click that file in filemanager how many times u want and nothing happens.
I already checked the files on windows and they seems to be both same. MimeType is correct. Any ideas how to fix the problem? Thanks
public void enableDownload() {
myWebView = findViewById(R.id.webview);
myWebView.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
if (hasStoragePermission() == false) {
requestStoragePermission();
} else {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}
}
});
}
Permissions in mainActivity:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
EDIT:
Download is behind log in so changing to Intent ( download over chrome ) is not possible.

Download Path of file from a website loaded in WebView Android

I am working on a project where use case is we have to load a web page url in webview which has a button when user clicks on button it will download pdf file. And we have to open that downloaded PDF. currently, i am not finding which path does the file got Downloaded.
You can use the webview download listener and set the path manually using DownloadManager.
An example where the file will be download to /Downloads folder would be:
myWebview.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimeType);
request.allowScanningByMediaScanner();
String fileExtenstion = MimeTypeMap.getFileExtensionFromUrl(url);
String filename = URLUtil.guessFileName(url, contentDisposition, fileExtenstion);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
Also add the below permissions to AndroidManifest.xml .
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Android: Downloading in webview not working in oreo, it force closes in Oreo devices

Download manager code is not working in Android WebView for Oreo devices, but it's working well for older versions
In case other than Oreo devices it toasts "downloading file" and it gets downloaded but in case of Oreo it force closes (crash)
Below is the code I am using (in fragment)
webView.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
DownloadManager dm = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getActivity().getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
Try to remove the preloading of fonts by removing
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
Source Webview in Oreo not working
This is the code what I figured it out and working for me.
NOTE: code is been executed in a fragment, for the main activity remove getActivity().
in manifest give the permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Tutorial for permision prompt
CODE:
webView.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
if(Build.VERSION.SDK_INT <= 26 ){
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
}
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
DownloadManager dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getActivity().getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});

Android Webview Download manager To Download PDF Files

I have an android application which uses a single WebView which loads a website inside it.
setContentView(R.layout.activity_main);
WebView webView = (WebView) this.findViewById(R.id.webview);
Then i used download manager to Download my files from server
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
// ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
//for downloading directly through download manager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
Environment.getExternalStorageDirectory();
getApplicationContext().getFilesDir().getPath(); //which returns the internal app files directory path
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
when i try to download a file which is created dynamically by the website i get to download the websites HTML instead of the PDF.
webView.loadUrl("http://bookboon.com/");
Thanks in Advance
Actually that HTML page is showing the login page for authentication. So you need a session key which is inside the cookies of that website.
So you need to add three lines in onDownloadListener
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
// ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
//for downloading directly through download manager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//This three lines will do your work
CookieManager cookieManager = CookieManager.getInstance();
String cookie = cookieManager.getCookie("<Your website url>"); // which is "http://bookboon.com"
request.addRequestHeader("Cookie", cookie);
//................................................
request.allowScanningByMediaScanner();
Environment.getExternalStorageDirectory();
getApplicationContext().getFilesDir().getPath(); //which returns the internal app files directory path
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});

File Not Getting Download Within WebView

I am working with an android application where i need to open a form URL in my webview. I am able to open the form in webview, there is a submit button with form, click on this button will download the file within webview but its not happening. I am able to download the file with normal browser on mobile like google chrome. How can i download the file within my application webview.
i don't know how the you are trying, but you can use this code to download the file inside the webview
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
intent.setType("*/*");//any application,any extension
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
and add this permission to your android manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
hope it will help.!

Categories

Resources