Unable to download Video file in WebView - android

When I click the link in WebView then video downloading is started but the format of video is unknown, due to which Android is not playing the downloaded video file.
Also, when I download the video from the same link in chrome then video download in correct format and android is able to play the video file.
WebView webview = (WebView) findViewById(R.id.web1);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("my url");
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.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Game of thrones ");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});

Related

How to save image through webview?

Suppose I'm using a URL in webview http://demo.io and this URL has a button of camera and when I click on that camera button then my URL became http://demo.io/captureImage. Then I have an issue when I check the url of webview then it shows only http://demo.io not showing http://demo.io/captureImage so how I can get this URL in Android. Because this URL has an image and I wanna save this image. When this URL (http://demo.io/captureImage) runs on chrome and I capture the image then captured image downloaded but in Android webview image after capturing not downloading. Please suggest me appropriate answer or code
You can add a download listner to webivew and download the image from the url as shown below
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.setMimeType(mimeType);
//------------------------COOKIE!!------------------------
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
//------------------------COOKIE!!------------------------
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);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}
If I get your issue correctly,
First you have to reset WebView with new url. Try below to do this,
cameraButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
UrActivity.this.mWebView.loadUrl("http://demo.io/captureImage");
}});
Second is to download image you can use setDownloadListener(...) as suggested by #Hasif Seyd

Android webview downloading pdf, xlsm as html file

I convert my website into an android webview app. Everything is working fine but when I am trying to download any file from my website the file is downloading in HTML format. But the files were downloading in correct format when I tried to download from a pc or mobile browser.
webView.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request myRequest = new DownloadManager.Request(Uri.parse(url));
myRequest.allowScanningByMediaScanner();
myRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager myManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Objects.requireNonNull(myManager).enqueue(myRequest);
Toast.makeText(MainActivity.this,"Download Started....", Toast.LENGTH_SHORT).show();
}
}

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

webview, download file on finish prepared document

I'm using webview android.
I want to do basically is to download a file, when the server offers it to me.
Whatever happens, that until just the last post, "in a browser" does not give you the option to save the file.
Therefore, when I try to save the file in android, I never get save.
As you can see, it does not end until the last post, the browser does not have the file. It seems a method asynchronous
webView.setDownloadListener(new DownloadListener()
{
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
{
//for downloading directly through download manager
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);
}
});
And these code, but can't open in browser because have Get and Post request to download PDF.
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
I can not think of other ways

Categories

Resources