Search code examples
androidandroid-webviewandroid-permissionsandroid-download-manager

Ask for permission when clicked on download button


I am making an app with webView in Android studio. It contains pictures only so I also have an integrated download button in my app. In android Marshmallow version you have to ask for permission and I have a permission code in onCreate like this:

int check = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (check == PackageManager.PERMISSION_GRANTED) {
        //Do something
    } else {
        requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},1024);
    }

but it pops up when the app starts and I want to pop it up when the user clicks on the download button.

my download button in html looks like this:

<a href="url">Download</a>

and the download code in app looks like this:

 @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.endsWith(".jpg")) {
                    Uri source = Uri.parse(url);
                    // Make a new request pointing to the .apk url
                    DownloadManager.Request request = new DownloadManager.Request(source);
                    // appears the same in Notification bar while downloading
                    request.setDescription("Description for the DownloadManager Bar");
                    request.setTitle("PunjabiDharti.jpg");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                        request.allowScanningByMediaScanner();
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    }
                    // save the file in the "Downloads" folder of SDCARD
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "PunjabiDharti.jpg");
                    // get download service and enqueue file
                    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    manager.enqueue(request);

How to pop up the request permission when the user clicks on the download button ?


Solution

  • You can achieve it as follows:

    public class WebViewActivity extends AppCompatActivity {
    
        public static final int DOWNLOAD_REQUEST_CODE= 1024;
        @Override
        protected void onCreate(final Bundle savedInstanceState) {
        ...
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.endsWith(".jpg")) {
                    int check = ActivityCompat.checkSelfPermission(this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
                    if (check == PackageManager.PERMISSION_GRANTED) {
                    //DO the download stuff here
                    } else {
                      requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},DOWNLOAD_REQUEST_CODE);
                    }
                }
            }
         });
       }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            if(requestCode == DOWNLOAD_REQUEST_CODE && grantResults[0] == PackageManager.PERMISSION_GRANTED){
               //DO the download stuff here
            }
        }
    }