Here is the new updated code but still give me the error "Unauthorized.You do not have permission for this action" I want to know why this error thanks. I have implemented the recommended answers still didnt work.
public class TipsActivity extends AppCompatActivity {
private WebView mWebView;
private static final int REQUEST_CODE_PERMISSION = 2;
private static String[] PERMISSIONS_REQ = {
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private static boolean verifyPermissions(Activity activity) {
// Check if we have write permission
int WritePermision = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (WritePermision != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_REQ,
REQUEST_CODE_PERMISSION
);
return false;
} else {
return true;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips);
mWebView = (WebView) findViewById(R.id.webView7);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadUrl("http://www.example.com");
mWebView.setWebViewClient(new WebViewClient());
mWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimeType,
long contentLength) {
if (verifyPermissions(TipsActivity.this)) {
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_LONG).show();
} else {
//prompt user for permission
}
}
});
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
Here is the new updated code but still give me the error "Unauthorized.You do not have permission for this action" I want to know why this error thanks. I have implemented the recommended answers still didnt work.