-1
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);
        }
    });



}

I have made a webview app and i want to download videos from my webview app and i have add the download function code above but this code is not working when i press download button my app crashes

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

No permission to write to /storage/emulated/0/Download/Download: Neither user 10143 nor current process has android.permission.WRITE_EXTERNAL_STORAGE

Go to AndroidManifest.xml and above of <application tag add:

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

It seems like you'll need runtime permission too. Add the following codes inside your onDownloadStart for example:

@Override
public void onDownloadStart(String s, String s1, String s2, String s3, 
             long l) {

                   if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                // permission has not been granted.
                 // Request for the permission like adding a function which will do that

                  requestPermission(); // a function to request permissions

            } else {

               // Permission granted!! do your stuff here

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


          }
 });

Function to request permission:

private void requestPermission() {
        Log.i(TAG, "permission has NOT been granted. Requesting permission.");
            private static final int REQUEST_EXTERNAL = 0;


        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example if the user has previously denied the permission.
            Log.i(TAG,
                    "Displaying permission rationale to provide additional context.");
            Snackbar.make(mLayout, R.string.permission_camera_rationale,
                    Snackbar.LENGTH_INDEFINITE)
                    .setAction(R.string.ok, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ActivityCompat.requestPermissions(MainActivity.this,
                                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                    REQUEST_EXTERNAL);
                        }
                    })
                    .show();
        } else {

            // permission has not been granted yet. Request it directly.
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_EXTERNAL);
        }

    }

Check this link: https://developer.android.com/training/permissions/requesting

And this: Neither user 10102 nor current process has android.permission.READ_PHONE_STATE

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108