webview上传图片经验分享

用webview显示html界面,上传功能默认是没有的,也就是点击上传按钮没有任何反应,这时候要重新webview中webchromeclient的openfilechooser方法(其实webchromeclient里没有openfilechooser这个方法,不过不要怀疑,添加这个方法就可以了,他内部会调用到的);

接下来说下2.3和4.0系统的情况下,这两个系统在上传所指向的链接会稍微有点不一样,就以拍照和选择本地图片为例,2.3系统的情况下,上传的路径是以content://开头的路径来指向(如:content://......../1),但在4.0系统时,拍照所产生的路径不能是content://开头的路径了,而是以一个用文件路径(如:/sd/pricure/icon.jpg),以上是我研究的经验,代码附上:

public class BbsWebChromeClient extends WebChromeClient {
    Context mContext;
    ValueCallback<Uri> mUploadMessage;
    public static final String TAG = "BbsWebChromeClient";
    public static final int PICTURE_REQUEST = 100111;
    PhotoUtil mPhotoUtil; //自定义弹窗
    String localTempImageFileName = ""; 临时文件
    int mPhoneVersion = 2;
    String path;
    File file;
    String pictureName = "temp_picture.jpg";
    int mCurrentType;

    public BbsWebChromeClient(Context context) {
        mContext = context;
        mPhotoUtil = new PhotoUtil(context, null);
        String[] localVersion = WebUtil.getSystemNum().split("\\.");
        try {
            mPhoneVersion = Integer.valueOf(localVersion[0]);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            QEngine.logger.debug("出错了");
        }
        String sdCardPath = FileUtils.getSDCardPath();
        if (sdCardPath != null)
            path = sdCardPath + File.separator + Constants.SDPath;
    }

    // For Android 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        uploadPicture(uploadMsg);
    }

    // For Android > 4.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg,
            String acceptType, String capture) {
        uploadPicture(uploadMsg);
    }

    // Andorid 3.0 +
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        uploadPicture(uploadMsg);
    }

    private void uploadPicture(ValueCallback<Uri> uploadMsg) {
        mUploadMessage = uploadMsg;
        ModifyAvatarDialog modifyAvatarDialog = new ModifyAvatarDialog(
                mContext, R.style.MyDialog) {
            // 选择本地相册
            @Override
            public void doGoToImg() {
                this.dismiss();
                mCurrentType = PhotoUtil.FLAG_CHOOSE_IMG;
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                ((Activity) mContext).startActivityForResult(
                        Intent.createChooser(intent, "完成操作需要使用"),
                        PICTURE_REQUEST);
            }

            // 选择相机拍照
            @Override
            public void doGoToPhone() {
                this.dismiss();
                mCurrentType = PhotoUtil.FLAG_CHOOSE_PHONE;
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (mPhoneVersion >= 2
                        && mCurrentType == PhotoUtil.FLAG_CHOOSE_PHONE) {
                    QEngine.logger.debug("mPhoneVersion >= 3");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(new File(path, pictureName)));
                }
                ((Activity) mContext).startActivityForResult(intent,
                        PICTURE_REQUEST);
            }

            @Override
            public void doCancle() {
                super.doCancle();
                mUploadMessage.onReceiveValue(null);
                mUploadMessage = null;
            }
        };
        Display display = ((Activity) mContext).getWindowManager()
                .getDefaultDisplay();
        modifyAvatarDialog.getWindow().getAttributes().width = (int) display
                .getWidth();
        modifyAvatarDialog.show();
        modifyAvatarDialog.setCanceledOnTouchOutside(false);
        modifyAvatarDialog
                .setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode,
                            KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_BACK
                                && event.getRepeatCount() == 0) {
                            if (keyCode == KeyEvent.KEYCODE_BACK
                                    && event.getRepeatCount() == 0) {
                                dialog.dismiss();
                                mUploadMessage.onReceiveValue(null);
                                mUploadMessage = null;
                            }
                        }
                        return false;
                    }
                });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PICTURE_REQUEST && resultCode == Activity.RESULT_OK) {
            if (null == mUploadMessage)
                return;
            Uri result = data == null || resultCode != Activity.RESULT_OK ? null
                    : data.getData();
            if (mPhoneVersion >= 2
                    && mCurrentType == PhotoUtil.FLAG_CHOOSE_PHONE) {// 4.0系统以上的拍照要用/sd/..路径
                mUploadMessage.onReceiveValue(Uri.fromFile(new File(path,
                        pictureName)));
            } else {
                mUploadMessage.onReceiveValue(result);// 其他使用content://的路径
            }
        } else {
            mUploadMessage.onReceiveValue(null);
        }
        mUploadMessage = null;
    }
}

以上代码已测试成功!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值