WebView在上传后将图像显示为文本

时间:2016-03-16 15:41:26

标签: android android-intent webview onactivityresult

我有WebView用于上传图片的界面。上传WebView后必须显示较小的上传图片,但它会显示一系列符号。

期待与现实

Screenshot

enter image description here

    web = (WebView) findViewById(R.id.account_webview);
    web.getSettings().setJavaScriptEnabled(true);
    web.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
    web.setWebChromeClient(new WebChromeClient());
    web.getSettings().setBuiltInZoomControls(true);
    web.getSettings().setDisplayZoomControls(true);

    web.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (url.substring(url.length() - 3, url.length()).equals("pdf")) {
                Log.e("shouldOverrideUrlLoadi", "pdf ");
                web.loadUrl("https://docs.google.com/gview?embedded=true&url=" + url);
            } else if (url.equals(getResources().getString(R.string.urlHome)) || url.equals(getResources().getString(R.string.urlPaymentIndex))
                    || url.equals(getResources().getString(R.string.urlDetailIndex))) {
                overridePendingTransition(R.anim.back_left_out, R.anim.back_right_in);
            } else{
                web.loadUrl(url);
            }

            return false;
        }

    });
    web.setWebChromeClient(new WebChromeClient() {
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {

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

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

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

        }

    });

onActivityResult

 @Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent intent) {
    Log.e("Result"," "+requestCode+" "+resultCode);
    if(requestCode==FILECHOOSER_RESULTCODE)
    {

        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        if (null == mUploadMessage) return;
        Uri result = intent == null || resultCode != RESULT_OK ? null
                : intent.getData();
        Log.e("Result"," "+result);
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您已将图像转换为String,然后将图像传输到服务器

intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());      

因此,请检查您是否正在显示图像的图像或字符串格式