单击下载按钮时请求许可

时间:2018-05-18 06:12:39

标签: android android-webview android-permissions android-download-manager

我在Android工作室使用webView制作应用程序。它只包含图片,所以我的应用程序中也有一个集成的下载按钮。在android Marshmallow版本中,你必须要求许可,我在onCreate中有一个权限代码,如下所示:

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>

并且app中的下载代码如下所示:

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

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

2 个答案:

答案 0 :(得分:1)

您可以按如下方式实现:

CREATE TABLE public.test(
id bigint NOT NULL DEFAULT nextval('test_id_seq'::regclass),
id1 integer NOT NULL,
id2 integer NOT NULL,
CONSTRAINT test_pkey PRIMARY KEY (id),
CONSTRAINT test_check CHECK (id1 <> id2)
);

CREATE UNIQUE INDEX test_id1_id2_unique
ON public.test
USING btree
((LEAST(id1, id2)), (GREATEST(id1, id2)));

答案 1 :(得分:0)

  

如果您正在使用webview那么..,也可以使用HTML5来实现,而不是获取权限的Android代码,然后检查是否授予权限,然后保存到某个位置和.....

你可以做的是......设置most of the properties的{​​{1}} webviewjavascript以及更多developers documentation official google site

现在您的webviewble to do the most of the things that browsers can do所以:

直接在HTML中点击/触摸下载图片AS:

<!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>

   

您可以在Click to see how it will work

找到它

并且不要忘记设置webview的设置,以使其能够像浏览器一样工作;设置can be find here

相关问题