Android webview如何上传文件和添加进度条

1.xml文件中添加
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/bannerWidgetId"
        >
        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:id="@+id/myProgressBar"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_marginTop="-3dp"
            android:max="100"
            android:maxHeight="5dp"
            android:minHeight="5dp"
            android:progress="0"
            />
        <WebView
            android:id="@+id/webviewId"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:layerType="hardware"/>
</RelativeLayout>
2.java代码中获取webview和(ProgressBar)
myProgressBar = (ProgressBar) findViewById(R.id.myProgressBar);
webviewId = (WebView) findViewById(R.id.webviewId);

webviewId.setWebChromeClient(new MyWebChromeClient())

3.实现MyWebChromeClient类并且继承WebChromeClient
class MyWebChromeClient extends WebChromeClient {

        //The undocumented magic method override
        //Eclipse will swear at you if you try to put @Override here
        // Android 3.0 版本支持打开文件
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);

        }
        // 设置webview加载ProgressBar从开始到结束
        @Override
         public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                myProgressBar.setVisibility(View.GONE);
            } else {
                if (myProgressBar.getVisibility() == View.GONE)
                    myProgressBar.setVisibility(View.VISIBLE);
                myProgressBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

        //Android 4.0 版本支持打开文件
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
            // getActivity().startActivityForResult( Intent.createChooser( i, "File Chooser" ), FILECHOOSER_RESULTCODE );

        }

        //Android 5.0 版本支持打开文件
        @Override
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
//    fileChooserParams.getAcceptTypes();
            mUploadMessages = filePathCallback;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);

            return true;
        }


    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值