android_WebView

来源:互联网 发布:网络推广收费骗局 编辑:程序博客网 时间:2024/05/29 13:47
WebView 增加横向进度条

        progressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleHorizontal);

        progressBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 5, 0, 0));

        addView(progressBar);

WebView 设置属性

        WebSettings ws = this.getSettings();

        ws.setJavaScriptEnabled(true);//允许javascript

        ws.setAllowFileAccess(true);启用或禁用WebView访问文件数据

        ws.setBuiltInZoomControls(false);设置是否支持缩放

        ws.setSupportZoom(true);设置是否支持变焦

        ws.setDomStorageEnabled(true);

        ws.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);设置布局方式

        ws.setUserAgentString("");

setWebViewClient()

                @Override

                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    //覆盖loadUrl方法,默认以浏览器打开URL

                }

               @Override

                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                    //失败回调

                    view.loadUrl("file:///android_asset/error.html");

                }

setWebChromeClient()

                @Override

                public void onProgressChanged(WebView view, int newProgress) {

                    if (newProgress == 100) {//加载进度条

                        progressBar.setVisibility(GONE);

                    } else {

                        if (progressBar.getVisibility() == GONE) {

                            progressBar.setVisibility(VISIBLE);

                        }

                        progressBar.setProgress(newProgress);

                    }

                }

                网页弹出框重写

                public boolean onJsAlert()

                onJsConfirm()

                onJsPrompt()

                //响应网页File标签,现实图片上传

                // 3.0 + 调用这个方法

                public void openFileChooser(ValueCallback<Uri> uploadMsg,String acceptType) {

                mUploadMessage = uploadMsg;//mUploadMessage 全局变量

                //跳转系统相册

                Intent intent = new Intent(Intent.ACTION_PICK);

                intent.setType("image/*");// 相片类型

                BaseActivity.currentActivity().startActivityForResult(intent,                                                                                                                                      WebViewActivity.FILECHOOSER_RESULTCODE);

                //跳转系统相机

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                BaseActivity.currentActivity().startActivityForResult(intent,

                                                                  WebViewActivity.FILECHOOSER_RESULTCODE);

                }

                // For Android  > 4.1.1

                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {

                    openFileChooser(uploadMsg, acceptType);

                }

                // Android < 3.0 调用这个方法

                public void openFileChooser(ValueCallback<Uri> uploadMsg) {

                    openFileChooser(uploadMsg, "");

                }

javascript调用本地方法

this.addJavascriptInterface(new JSAndroidBridge(this), "android");

执行JS方法,可通过参数向JS当前页面传值

public void executeJSMethod(String methodName, String param) {

        this.superLoadUrl("javascript:" + methodName + "('" + param + "')");

}

可供JS调用的类

public final class JSAndroidBridge {

        private JSBridgeWebView webView;

        public JSAndroidBridge(JSBridgeWebView webView) {

            super();

            this.webView = webView;

        }

        @JavascriptInterface

        public void refreshHomeActivityList() {//js可以通过方法名调用此方法

           //do something

        }

}

图片上传

    1.实现上面提到的openFileChooser(部分Android4.4.2不响应此方法)

    2.当前场景中onActivityResult中获取URI

    3.uri不为空是从相册获取的图片,为空是从相机获得图片

    4.部分相机获取的图片是缩略图

    5.部分手机从系统获取的图片是横向

@Override

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

        if (requestCode == FILECHOOSER_RESULTCODE) {

            mUploadMessage = .getWebView().getmUploadMessage();

            if (null == mUploadMessage)

                return;

            Uri uri = intent == null || resultCode != RESULT_OK ? null : intent.getData();

            }

            mUploadMessage.onReceiveValue(uri);//关键代码

        }

}

图片上传过程中得问题及解决方案
Android4.4.2 不响应openFileChooser方法

    上面设置的setUserAgentString()方法中将当前系统版本传到页面,页面判断为4.4.2系统不使用File标签上传图片,改为本地调用上传图片方法,上传成功后返回的图片路径,调用js的方法通知页面图片上传成功

选取图片操作bitmap, OOM

    BitmapFactory.Options opt = new BitmapFactory.Options();

    opt.inJustDecodeBounds = true;//只获取图片参数

    BitmapFactory.decodeFile(img_path, opt);

    if (opt.outWidth > 1000 || opt.outHeight > 1000) {

    opt.inSampleSize = 5;//缩小1/5

    }

    opt.inJustDecodeBounds = false;

    Bitmap bitmap = BitmapFactory.decodeFile(img_path, opt);

判断图片的方向

 //获取图片的路径

private String getImgPath(Uri uri){

        String img_path = "";

        if(uri != null){

            String[] proj = { MediaStore.Images.Media.DATA };

            Cursor actualimagecursor = managedQuery(uri,proj,null,null,null);

            int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            actualimagecursor.moveToFirst();

            img_path = actualimagecursor.getString(actual_image_column_index);

        }

        return img_path;

    }

//获取图片的存储角度

private int getBitmapDegree(String path) {

        int degree = 0;

        try {

            // 从指定路径下读取图片,并获取其EXIF信息

            ExifInterface exifInterface = new ExifInterface(path);

            // 获取图片的旋转信息

            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,

                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {

                case ExifInterface.ORIENTATION_ROTATE_90:

                    degree = 90;

                    break;

                case ExifInterface.ORIENTATION_ROTATE_180:

                    degree = 180;

                    break;

                case ExifInterface.ORIENTATION_ROTATE_270:

                    degree = 270;

                    break;

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        return degree;

    }

//旋转图片

public Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {

        Bitmap returnBm = null;

        // 根据旋转角度,生成旋转矩阵

        Matrix matrix = new Matrix();

        matrix.postRotate(degree);

        try {

            // 将原始图片按照旋转矩阵进行旋转,并得到新的图片

            returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);

        } catch (OutOfMemoryError e) {

        }

        if (returnBm == null) {

            returnBm = bm;

        }

        if (bm != returnBm) {

            bm.recycle();

        }

        return returnBm;

    }


0 0