How to Enable Download Button in webview Android Studio

अगर आपने android studio पर अपनी website को android app में convert किया है। और उसमें किसी फ़ाइल को Download करने पर वह Download नहीं हो रही है। तो इसका solution लेकर आये है। नीचे दिए गए android studio web view source code को आप अपने source code में add करके उसमें Download button को enable कर सकते हैं। इस code को add करने का वीडियो भी आप देख सकते हैं।  

How to Enable Download Button in webview Android Studio - newshank.com




यहां आपको दो फ़ाइल मिलेंगी जिन्हें आपको अपने web view source code में add करना है।  

1. ये code आपको AndroidManifest.xml में add करना है।  

---------------------------------------

AndroidManifest.xml


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

2. इस code को MainActivity.java में add करना है। यहाँ आपको कुछ error show हो सकते हैं। जिन्हें आपको menualy fix करना होगा आप बारी बारी mouse का corcer error पर लेकर जाएंगे वहां आपको import path show होगा उसपर आपको click कर देना है। इसी तरह आपको सभी error fix करने हैं।  

Download Button in webview को Android Studio में add करने में आ रही किसी भी समस्या के लिए वीडियो देखें। और Channel Subscribe कर लें।  


---------------------------------------

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


            }
        });

Tags