【发布时间】:2017-09-20 09:54:03
【问题描述】:
当我从我的应用程序下载文件时,任何人都可以帮助我询问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();
}
}
}
【问题讨论】:
标签: android webview android-permissions