Android WebView 上传图片文件有坑

首先说下这个坑是怎么来的,本来要在项目中集成一个第三方提供的功能,但是第三方没有SDK,只给了个H5的页面,只能用webview给套进去了,本来以为事情很简单,分分钟的事情,可是页面里面竟然有图片上传的功能,这也没啥,网上找了段代码加上就OK了,可是本人的手机就是掉不起来文件选择对话框,谷歌和百度一番之后,坑出现了,并且对我这个情况无解,那就是安卓版本4.4、4.4.1、4.4.2这几个版本不被支持,除非H5是自己写的,可以Cordova实现,挖坑的就是谷歌这货自己。谁看到了这个文章,并且有解决办法,可以回复我,本人感激不尽。最后,上点源码,调试过的,谁想用可以直接拿去(包含了cookie处理),但是记得判断安卓版本号,源码如下:

public class KengActivity extends BaseActivity implements View.OnClickListener {

    private WebView webView;
    private ImageView ivBack;
    private String url;

    public final static int FILECHOOSER_RESULTCODE = 1;
    public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2;
    public ValueCallback<Uri> mUploadMessage;
    public ValueCallback<Uri[]> mUploadMessageForAndroid5;

    @Override
    public void onCreateActivity() {
        setContentView(R.layout.activity_lanjingling_pay);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        url = getIntent().getExtras().getString("url");
        initView();
        initData();
    }

    private void initView() {
        webView = (WebView) findViewById(R.id.webView);
        ivBack = (ImageView) findViewById(R.id.ivBack);
    }

    private void initData() {

        WebSettings webViewSettings = webView.getSettings();
        webViewSettings.setJavaScriptEnabled(true);
        webViewSettings.setSupportZoom(false);
        webViewSettings.setBuiltInZoomControls(false);
        webViewSettings.setAppCacheEnabled(false);
        webViewSettings.setLoadWithOverviewMode(true);
        webView.canGoBack();
        webView.setWebChromeClient(new MyWebChromeClient());
        /**
         * 设置webview支持https
         */
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });

        /**
         *  给webview跳转的url添加cookie
         */
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);

        PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
        List<HttpCookie> cookies = myCookieStore.getCookies();
        for (int i = 0; i < cookies.size(); i++) {
            HttpCookie httpCookie = cookies.get(i);
            if (httpCookie.getName().equals("PHPSESSID")) {
                cookieManager.setCookie(url, httpCookie.getName() + "=" + httpCookie.getValue());
            }
        }
        CookieSyncManager.getInstance().sync();
        webView.loadUrl(url);
        LoadingDialog.showProgressDialog(context);
    }


    private class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                LoadingDialog.closeProgressDialog();
            }
            super.onProgressChanged(view, newProgress);
        }

        public void openFileChooser(ValueCallback<Uri> uploadMsg) {

            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            KengActivity.this.startActivityForResult(
                    Intent.createChooser(i, "File Chooser"),
                    FILECHOOSER_RESULTCODE);

        }

        public void openFileChooser(ValueCallback uploadMsg,
                                    String acceptType) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            KengActivity.this.startActivityForResult(
                    Intent.createChooser(i, "File Browser"),
                    FILECHOOSER_RESULTCODE);
        }

        // For Android 4.1
        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/*");
            KengActivity.this.startActivityForResult(
                    Intent.createChooser(i, "File Chooser"),
                    FILECHOOSER_RESULTCODE);

        }

        @Override
        public boolean onShowFileChooser(WebView webView,
                                         ValueCallback<Uri[]> filePathCallback,
                                         FileChooserParams fileChooserParams) {
            openFileChooserImplForAndroid5(filePathCallback);
            return true;//重要,没有这个会报错
        }
    }

    private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg) {
        mUploadMessageForAndroid5 = uploadMsg;
        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("image/*");
        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
        chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
        chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
        startActivityForResult(chooserIntent,
                FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent intent) {

        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
                return;
            Uri result = intent == null || resultCode != RESULT_OK ? null: intent.getData();
            if (result == null) {
                mUploadMessage.onReceiveValue(result);
                mUploadMessage = null;
                return;
            }

            Bitmap bm = null;
            //外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
            ContentResolver resolver = getContentResolver();
            try {
                Uri originalUri = intent.getData(); // 获得图片的uri
                bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);
                // 这里开始的第二部分,获取图片的路径:
                String[] proj = {MediaStore.Images.Media.DATA};
                // 好像是android多媒体数据库的封装接口,具体的看Android文档
                Cursor cursor = managedQuery(originalUri, proj, null, null,null);
                // 按我个人理解 这个是获得用户选择的图片的索引值
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                // 将光标移至开头 ,这个很重要,不小心很容易引起越界
                cursor.moveToFirst();
                // 最后根据索引值获取图片路径
                String path = cursor.getString(column_index);
                Uri uri = Uri.fromFile(new File(path));
                mUploadMessage.onReceiveValue(uri);
            } catch (IOException e) {
                Log.e("TAG-->Error", e.toString());
            }
        } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5) {
            if (null == mUploadMessageForAndroid5)
                return;
            Uri result = (intent == null || resultCode != RESULT_OK) ? null: intent.getData();            
            if (result != null) {
                mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
            } else {
                mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
            }
            mUploadMessageForAndroid5 = null;
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尚俊飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值