单击下载按钮时请求许可
Ask for permission when clicked on download button
我正在 Android studio 中使用 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>
应用程序中的下载代码如下所示:
@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
}
}
}
If you are using the webview
then.., it can be achieved with HTML5
too, instead of android code fore asking permission, then checking if permission granted or not, then saving to some location and .....
您可以做的是.. 将 webview
中的 most of the properties
设置为 javascript
以及您可以在 developers documentation official google site
.
上找到的更多内容
现在你的 webview
是 ble 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
我正在 Android studio 中使用 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>
应用程序中的下载代码如下所示:
@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
}
}
}
If you are using the
webview
then.., it can be achieved withHTML5
too, instead of android code fore asking permission, then checking if permission granted or not, then saving to some location and .....
您可以做的是.. 将 webview
中的 most of the properties
设置为 javascript
以及您可以在 developers documentation official google site
.
现在你的 webview
是 ble 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