Downloaded APK files in android WebView not running - android

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.

Related

Download file in android studio with the original file name

How to write the code to download file in android studio with the original file name?
I am trying to create a very simple webview mobile android application as I am a beginner in this field. I am almost done but I am stuck at a problem. It is working perfectly with .pdf but it is saving .lrc files with .txt extension.
My code is:
MainActivity.java
webview.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String fileExtenstion, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
MimeTypeMap.getFileExtensionFromUrl(url);
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,fileExtenstion));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, fileExtenstion));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(),"Downloading File",Toast.LENGTH_SHORT).show();
}
}); ```
**AndroidManiFest.xml**
```
Can you guys help me to make it right please.

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);
}
});

how to open download manager by clicking?

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

Categories

Resources