单击下载按钮时请求许可



我正在Android工作室中用webView制作一个应用程序。它只包含图片,所以我的应用程序中还有一个集成的下载按钮。在安卓棉花糖版本中,您必须请求许可,我在创建中有一个权限代码,如下所示:

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

但是它在应用程序启动时弹出,我想在用户单击下载按钮时弹出它。

我在 HTML 中的下载按钮如下所示:

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

应用程序中的下载代码如下所示:

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

用户点击下载按钮时如何弹出请求权限?

您可以按如下方式实现它:

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
}
}
}
如果您

使用的是webview,那么..,也可以通过HTML5来实现,而不是Android代码预先询问许可,然后检查是否授予权限,然后保存到某个位置并.....


你能做的是..设置most of the propertieswebview,例如javascript,以及您可以在developers documentation official google site上找到的更多内容。


现在您的webview是一个ble to do the most of the things that browsers can do,因此:


直接实现在HTML中点击/触摸图片的下载:

<!DOCTYPE html>
<html>
<body>
<p>Click on the w3schools logo to download the image:<p>
<a href="/images/myw3schoolsimage.jpg" download>
<img border="0" src="/images/myw3schoolsimage.jpg" alt="W3Schools" width="104" height="142">
</a>
<p><b>Note:</b> The download attribute is not supported in Edge version 12, IE, Safari 10 (and earlier), or Opera version 12 (and earlier).</p>

你可以在这里找到它 点击看看它将如何工作


并且不要忘记设置webview的设置,使其能够像浏览器一样工作;设置可以在这里找到

最新更新