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);
}
- H5提供的资源访问接口
- Android原生的权限申请
- Android资源URI的定义
- Android拍照或者访问相册数据返回处理适配不同的Android系统手机
- chrom 查看H5运行内容:chrome://inspect
下一篇: H5调用Android图片上传功能