当我从我的应用程序下载文件时,任何人都可以帮我询问WRITE_External_STORAGE
的运行时权限吗?在android中使用我的Webview
显示我的网站,我有PDF文件,所以我需要运行时权限才允许在我的应用程序中存储访问..
以下是我的示例代码:
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
WebSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://learntatatrusts.org/");
myWebView.setWebViewClient(new WebViewClient());
myWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String
contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new
DownloadManager.Request(Uri.parse(url));
CookieManager cookieManager = CookieManager.getInstance();
String cookie =
cookieManager.getCookie("https://learntatatrusts.org/");
request.addRequestHeader("Cookie", cookie);
request.allowScanningByMediaScanner();
Environment.getExternalStorageDirectory();
getApplicationContext().getFilesDir().getPath(); //which returns
the internal app files directory path
request.setNotificationVisibility(DownloadManager.Request.
VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.
DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager)
getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
}
@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
答案 0 :(得分:3)
首先将此行添加到清单文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果已经授予了创建权限的检查权限,那么已经授予了权限,而不仅仅是加载了您的网址,如果没有,则要求获得许可。
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
// Function for check permission already granted or not
public static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
WebSettings.setJavaScriptEnabled(true);
// Permission check
if (!hasPermissions(context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE})) {
// Permission ask
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 111);
} else {
// if permission is already granted than load url
loadUrl();
}
}
@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
public void loadUrl() {
myWebView.loadUrl("https://learntatatrusts.org/");
myWebView.setWebViewClient(new WebViewClient());
myWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String
contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new
DownloadManager.Request(Uri.parse(url));
CookieManager cookieManager = CookieManager.getInstance();
String cookie =
cookieManager.getCookie("https://learntatatrusts.org/");
request.addRequestHeader("Cookie", cookie);
request.allowScanningByMediaScanner();
Environment.getExternalStorageDirectory();
getApplicationContext().getFilesDir().getPath(); //which returns the internal app files directory path
request.setNotificationVisibility(DownloadManager.Request.
VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.
DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager)
getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 111) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadUrl();
}
}
}
}