I'm encountering an issue with my webview app. I have a web app which uses JQuery Datatables with the buttons plugin (as seen from the image below)
I am using this web app in my webview application. when I click the "PDF" button. It should download a pdf file containing the data in the table. But what happens is that it is downloading a ".bin" file instead of a "pdf" file.
These are the permissions set in my AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
And this is the code for my webview app in MainActivity.java
public class MainActivity extends AppCompatActivity {
WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = findViewById(R.id.webView);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.setWebViewClient(new Callback());
web.loadUrl("http://webview.web-app.cf/");
web.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
try {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://webview.web-app.cf/"));
request.setMimeType(mimetype);
String cookies = CookieManager.getInstance().getCookie("http://webview.web-app.cf/");
request.addRequestHeader("cookie",cookies);
request.addRequestHeader("User-agent",userAgent);
request.setDescription("Downloading File . . .");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName("http://webview.web-app.cf/",null,null));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(),"Downloading File",Toast.LENGTH_LONG).show();
}catch (Exception e){
if (ContextCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(getBaseContext(), "To download attachments, Please tap allow when prompted", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
110);
} else {
Toast.makeText(getBaseContext(), "To download attachments, Please tap allow when prompted", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
110);
}
}
}
}
});
}
private class Callback extends WebViewClient {
@Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event){
return false;
}
}
}
I'm not really familiar with java so I'm not sure what is missing in my code to achieve my goal.
Thanks in advance for the help.
