Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在webview中下载文档或文件_Android - Fatal编程技术网

Android 如何在webview中下载文档或文件

Android 如何在webview中下载文档或文件,android,Android,这是新更新的代码,但仍然给我错误“Unauthorized.You not permission for this action”我想知道为什么会出现此错误,谢谢。我已经实现了建议的答案,但仍然不起作用 公共类提示活动扩展了AppCompative活动{ 私有网络视图; 私有静态最终整数请求\代码\权限=2; 私有静态字符串[]权限\u请求={ Manifest.permission.WRITE\u外部存储 }; 私有静态布尔验证权限(活动){ //检查我们是否有写权限 int WritePe

这是新更新的代码,但仍然给我错误“Unauthorized.You not permission for this action”我想知道为什么会出现此错误,谢谢。我已经实现了建议的答案,但仍然不起作用

公共类提示活动扩展了AppCompative活动{
私有网络视图;
私有静态最终整数请求\代码\权限=2;
私有静态字符串[]权限\u请求={
Manifest.permission.WRITE\u外部存储
};
私有静态布尔验证权限(活动){
//检查我们是否有写权限
int WritePermision=ActivityCompat.checkSelfPermission(活动、清单、权限、写入外部存储);
if(writepermission!=已授予PackageManager.PERMISSION){
//我们没有权限,因此请提示用户
ActivityCompat.requestPermissions(
活动,
权限请求,
请求\u代码\u权限
);
返回false;
}否则{
返回true;
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips);
mWebView=(WebView)findViewById(R.id.webView7);
WebSettings WebSettings=mWebView.getSettings();
setJavaScriptEnabled(true);
setWebChromeClient(新的WebChromeClient());
mWebView.loadUrl(“http://www.example.com");
setWebViewClient(新的WebViewClient());
setDownloadListener(新的DownloadListener(){
@凌驾
public void onDownloadStart(字符串url、字符串userAgent、,
字符串contentDisposition,字符串mimeType,
长(长){
if(验证权限(TipsActivity.this)){
DownloadManager.Request=new DownloadManager.Request(
parse(url));
request.setMimeType(mimeType);
字符串cookies=CookieManager.getInstance().getCookie(url);
addRequestHeader(“cookie”,cookies);
addRequestHeader(“用户代理”,userAgent);
setDescription(“下载文件…”);
request.setTitle(URLUtil.guessFileName)(url、contentDisposition、,
mimeType);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.request.VISIBILITY\u VISIBLE\u NOTIFY\u完成);
request.SetDestinationNexternalPublicDir(
Environment.DIRECTORY_下载,URLUtil.guessFileName(
url、contentDisposition、mimeType);
DownloadManager dm=(DownloadManager)getSystemService(下载服务);
dm.enqueue(请求);
Toast.makeText(getApplicationContext(),“下载文件”,
Toast.LENGTH_LONG).show();
}否则{
//提示用户输入权限
}
}
});
}
@凌驾
public void onBackPressed(){
if(mWebView.canGoBack()){
mWebView.goBack();
}否则{
super.onBackPressed();
}
}
}
您可以试试-

mWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
});
你可以试试-

mWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
});

注意添加在清单中写入外部存储的权限,并获取用户对post lolipop api设备的运行时权限

对于运行时任务,请检查此

在活动中,首先将此声明为字段(全局变量)

在您的下载管理器中使用上述方法,有点像这样

 mwebView.setDownloadListener(new DownloadListener()
       {

      @Override  
       public void onDownloadStart(String url, String userAgent,
            String contentDisposition, String mimeType,
            long contentLength) {
if(verifyPermissions(yourActivity.this)){
DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(url));
        request.setMimeType(mimeType);
        String cookies = CookieManager.getInstance().getCookie(url);
        request.addRequestHeader("cookie", cookies);
        request.addRequestHeader("User-Agent", userAgent);
        request.setDescription("Downloading file...");
        request.setTitle(URLUtil.guessFileName(url, contentDisposition,
                mimeType));
        request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                        url, contentDisposition, mimeType));
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
        Toast.makeText(getApplicationContext(), "Downloading File",
                Toast.LENGTH_LONG).show();
}else{
//prompt user for permission
}

    }});

有关更多信息,请检查注意添加在清单中写入外部存储的权限,并获取用户对post lolipop api设备的运行时权限

对于运行时任务,请检查此

在活动中,首先将此声明为字段(全局变量)

在您的下载管理器中使用上述方法,有点像这样

 mwebView.setDownloadListener(new DownloadListener()
       {

      @Override  
       public void onDownloadStart(String url, String userAgent,
            String contentDisposition, String mimeType,
            long contentLength) {
if(verifyPermissions(yourActivity.this)){
DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(url));
        request.setMimeType(mimeType);
        String cookies = CookieManager.getInstance().getCookie(url);
        request.addRequestHeader("cookie", cookies);
        request.addRequestHeader("User-Agent", userAgent);
        request.setDescription("Downloading file...");
        request.setTitle(URLUtil.guessFileName(url, contentDisposition,
                mimeType));
        request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                        url, contentDisposition, mimeType));
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
        Toast.makeText(getApplicationContext(), "Downloading File",
                Toast.LENGTH_LONG).show();
}else{
//prompt user for permission
}

    }});

更多检查

它给了我一个错误,说“你没有此操作的权限”,在我的android清单文件中,我有你应该使用运行时权限检查post lollipop Api所需的权限。对于读或写外部存储等用户敏感操作,棒棒糖前api从不需要权限。但是现在它必须被编码检查这个我怎么能在我的新代码中实现它this@user333210检查编辑后的答案我有什么:它给了我一个错误,说“你没有这个行动的权限”,在我的android清单文件中,我有你应该使用运行时权限检查它的要求后棒棒糖Api。对于读或写外部存储等用户敏感操作,棒棒糖前api从不需要权限。但是现在它必须被编码检查这个我怎么能在我的新代码中实现它this@user333210检查编辑后的回答此处我有什么:它给了我一个错误,说“你没有此操作的权限”在我的android manifest文件中,我有您测试的api级别?android版本为OreApi级别23及以上您需要在运行时请求权限我如何将其实现到我的代码中,我对此一无所知它给了我一个错误,说“您没有此操作的权限”在我的android清单文件中,我有您测试的api级别?android版本是OreApi级别23及以上,您需要在运行时请求许可我如何将其实现到我的代码中,我是新手