Weex初体验之模块(Module)-图片模块

模块(Module)-图片模块

模块:native实现相应的功能,提供给Weex端调用

本人挑几个特别的模块讲解下:

照片相关的模块

新建一个PhotoModule类,继承WXModule

private File mCurPhotoFile;

    private JSCallback mTaskPhotoBack;

    /**
     * 拍照
     *
     * @param callback 回调
     * @return true 成功 false 失败
     */
    @JSMethod
    @Override
    public boolean takePhoto(JSCallback callback) {
        mTaskPhotoBack = callback;

        Context context = mWXSDKInstance.getContext();

        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        mCurPhotoFile = new File(FileUtil.getImagePath(context),
                FileUtil.getPhotoFileName(context));

        Uri mImageUri = Uri.fromFile(mCurPhotoFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);

        final Activity activity = WXApplication.getInstance().getTopActivity();

        if (activity != null) {
            PermissionsUtil.getInstance(activity).requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, new PermissionsUtil.requestPermissionListener() {
                @Override
                public void onSuccess() {
                    activity.startActivityForResult(intent, 0x01);
                }

                @Override
                public void onFail() {
                    ToastUtil.getInstance().toast("打开相机失败!");
                    mTaskPhotoBack = null;
                }
            });
        } else {
            mTaskPhotoBack = null;
            return false;
        }

        return true;
    }

    /**
     * 选择照片
     *
     * @param callback 回调
     * @return true 成功 false 失败
     */
    @JSMethod
    @Override
    public boolean selectImage(JSCallback callback) {
        mTaskPhotoBack = callback;

        Intent intent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");

        Activity activity = WXApplication.getInstance().getTopActivity();

        if (activity != null) {
            activity.startActivityForResult(intent, 0x02);
        } else {
            mTaskPhotoBack = null;
            return false;
        }

        return false;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            if (requestCode == 0x01) {
                if (mCurPhotoFile.exists()) {
                    backPath(mCurPhotoFile.getPath());
                } else {
                    LogUtil.e("图片不存在! mCurPhotoFile 不存在");
                }
            } else if (requestCode == 0x02) {
                Uri mImageUri = data.getData();
                if (mImageUri == null) {
                    LogUtil.e("图片不存在! mImageUri = null");
                    return;
                }

                backPath(FileUtil.getImagePath(WXApplication.getInstance(), mImageUri));
            }
        }
        mTaskPhotoBack = null;
    }

    /**
     * 图片压缩回调
     *
     * @param path 图片路径
     */
    private void backPath(String path) {
        path = ImageUtil.imageCompress(path);
        
        //将拍照或者相册的图片路径添加标示头返回给Weex(这就是前面图片适配器文章说的添加标示头)
        if (mTaskPhotoBack != null) {
            if (!TextUtils.isEmpty(path)) {
                path = Constants.mCardImgHead + path;
            }
            mTaskPhotoBack.invoke(path);
        }
    }

当然咯,也是要向Weex注册模块

        try {
          //注册照片相关
            WXSDKEngine.registerModule("photoModule", PhotoModule.class);
        } catch (WXException e) {
            LogUtil.e(e.getMessage());
        }

Weex就可以调用拍照及选择照片啦


下一篇:微信分享模块

你可能感兴趣的:(Android,weex,Weex_Android)