欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

H5调用原生照片上传功能实现

程序员文章站 2022-05-30 21:44:21
...

原生调用相册和照相


    /**
     * 拍照
     */
    public void takePhotos(Activity activity, Uri contentUri, int callBackType) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);// 更改系统默认存储路径
        activity.startActivityForResult(takePictureIntent, callBackType);
    }

    /**
     * 从相册获取
     */
    public void photoAlbum(Activity activity, Uri contentUri, int callBackType) {
        Intent chooseIntent = new Intent(Intent.ACTION_GET_CONTENT);
        chooseIntent.addCategory(Intent.CATEGORY_OPENABLE);
        chooseIntent.setType("image/*");
        chooseIntent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
        activity.startActivityForResult(chooseIntent, callBackType);
    }

    public void chooseImageDialog(Activity activity, Uri contentUri, int callBackType) {
        //获取拍照
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);// 更改系统默认存储路径
        //获取照片资源
        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("*/*");
        Intent[] intentArray;
        if (takePictureIntent != null) {
            intentArray = new Intent[]{takePictureIntent};
        } else {
            intentArray = new Intent[0];
        }
        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
        chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
        activity.startActivityForResult(chooserIntent, callBackType);
    }
  1. H5提供的资源访问接口
  2. Android原生的权限申请
  3. Android资源URI的定义
  4. Android拍照或者访问相册数据返回处理适配不同的Android系统手机
  5. chrom 查看H5运行内容:chrome://inspect