-2
package com.example.shery.youtubevideodownloader;

 import android.Manifest;
 import android.app.DownloadManager;
 import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.os.Environment;
 import android.support.v4.app.ActivityCompat;
 import android.support.v4.content.ContextCompat;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.webkit.DownloadListener;
 import android.webkit.WebChromeClient;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
 import static android.content.pm.PackageManager.*;

 public class MainActivity extends AppCompatActivity {
 private WebView webView;
 String url = "https://svidzdownloader.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView)findViewById(R.id.webview);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl(url);
    WebSettings webSettings = webView.getSettings();



    webSettings.setJavaScriptEnabled(true);
    webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String s, String s1, String s2, String s3, long l) {
            DownloadManager.Request request = new DownloadManager.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);
        }
    });

}

}

even i have added permission on manifest.xml

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

I have a webview app and I want to download videos from it and I have added the download function on my code above so I added the permission on manifest but when I pressed download button my app crashes
this is my logcat "No permission to write to /storage/emulated/0/Download/Download: Neither user 10143 nor current process has android.permission.WRITE_EXTERNAL_STORAGE"

so how to take runtime permission

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

First you have to check if you already have the permission using this code:

if (ActivityCompat.checkSelfPermission(context, Manifest.permission. WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
  //Ask for permission
   ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
}else{
  // Do your work
  webView.setDownloadListener(new DownloadListener() {
    @Override
    public void onDownloadStart(String s, String s1, String s2, String s3, long l) {
        DownloadManager.Request request = new DownloadManager.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 now if the permission is granted then you have to override this method to know the status of permission.

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 100:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               //Do your work here.
               webView.setDownloadListener(new DownloadListener() {
    @Override
    public void onDownloadStart(String s, String s1, String s2, String s3, long l) {
        DownloadManager.Request request = new DownloadManager.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);
    }
});
            }

    }
}

Hope this helps.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23