Enable Download Button in Webview Android App (ANDROID STUDIO)

 

 

 

Enable Download Button in Webview Android App (ANDROID STUDIO). Please don’t forget to Like, Share, Comment and Subscribe to our Techno Vedant Channel for more videos.

Products I Use For Making Videos:
Mic: https://amzn.to/2sItJ8P
Tripod: https://amzn.to/2OKuBlF
Camera: https://amzn.to/2Rhfimc
Laptop: https://amzn.to/2Lis1kV
LED Tubelight: https://amzn.to/2RmEwQ2
Web Hosting Link: https://www.hostinger.in/vedant



MainActivity.Java

if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
            if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED){

                Log.d("permission","permission denied to WRITE_EXTERNAL_STORAGE - requesting it");
                String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
                requestPermissions(permissions,1);
            }


        }

//handle downloading

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);
                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);
                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_SHORT).show();


            }
        });


AndroidMainfest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Leave a Reply

Top