本文共 5702 字,大约阅读时间需要 19 分钟。
package com.zwk.ezandroid.util;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.Intent;import android.net.Uri;import android.os.Environment;import android.provider.MediaStore;import android.widget.Toast;import java.io.File;import static android.app.Activity.RESULT_CANCELED;/** * 拍照,照片选取,裁剪,统一封装 */public class WKCameraUtil { private static final int CAMERA_CODE = 1; private static final int GALLERY_CODE = 2; private static final int CROP_CODE = 3; public Activity act; private File photoFile; private Uri photoUri; public WKCameraUtil(Activity act){// this(act,new File(App.getInstance().getExternalCacheDir(), "ezandroid\tempPhoto.jpg")); this(act,new File(App.DirectoryImagePersonPhoto, "tempPhoto.jpg")); } public WKCameraUtil(Activity act, File file){ this.act=act; photoFile = file; } public void start(){ String[] type = { act.getResources().getString(R.string.GENERAL_CAMERA), act.getResources().getString(R.string.GENERAL_SELECT_PICTURE) }; new AlertDialog.Builder(act).setItems(type, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String state = Environment.getExternalStorageState(); if (!state.equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(act, "SDCard不可用,请打开文件读取权限", Toast.LENGTH_SHORT).show(); dialog.dismiss(); return; } switch (which) { case 0: chooseFromCamera(); break; case 1: chooseFromGallery(); break; } } }).show(); } /** * 从相册选择图片 */ private void chooseFromGallery() { photoUri = Uri.fromFile(photoFile); Intent intent = new Intent(Intent.ACTION_PICK); intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); act.startActivityForResult(intent, GALLERY_CODE); } // 拍照选择图片 private void chooseFromCamera() { // 调用系统相机 photoUri = FileUtil.getUriByFile(photoFile); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); act.startActivityForResult(intent, CAMERA_CODE); } public void onActivityResult(int requestCode, int resultCode, Intent data, WKCallback call) { if (resultCode == RESULT_CANCELED) { T.toast("取消"); return; } switch (requestCode) { case CAMERA_CODE: startImageZoom(photoUri); break; case GALLERY_CODE: startImageZoom(data.getData()); break; case CROP_CODE: call.onCall(Uri.fromFile(photoFile)); break; default: break; } } /** * 通过Uri传递图像信息以供裁剪 * @param uri */ private void startImageZoom(Uri uri) { // 构建隐式Intent来启动裁剪程序 Intent intent = new Intent("com.android.camera.action.CROP"); // 设置数据uri和类型为图片类型 intent.setDataAndType(uri, "image/*"); intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 显示View为可裁剪的 intent.putExtra("crop", true); // 裁剪的宽高的比例为1:1 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // 输出图片的宽高均为150 intent.putExtra("outputX", 150); intent.putExtra("outputY", 150); intent.putExtra("output", Uri.fromFile(photoFile)); // 裁剪之后的数据是通过Intent返回 intent.putExtra("return-data", false); act.startActivityForResult(intent, CROP_CODE); } public interface WKCallback{ void onCall(T t); }}
WKCameraUtil wkCamera;private OnClickListener linstener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.iv_person_photo: wkCamera = new WKCameraUtil(this); //wkCamera = new WKCameraUtil(this,new File("文件保存路径")); wkCamera.start(); break; } }}; protected void onActivityResult(int requestCode, int resultCode, Intent data) { wkCamera.onActivityResult(requestCode,resultCode,data,new WKCameraUtil.WKCallback() { @Override public void onCall(Uri photoUri) { Bitmap resultBitmap = BitmapUtil.getBitmap(photoUri, PersonCenterActivity.this); } });}public static Bitmap getBitmap(Uri photoUri, Context context) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable = true; options.inInputShareable = true; options.inPreferredConfig = Config.RGB_565; InputStream inputStream = null; Bitmap resultBitmap = null; try { inputStream = context.getContentResolver() .openInputStream(photoUri); resultBitmap = BitmapFactory.decodeStream(inputStream, null, options); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (null!=inputStream) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return srcBitmap;}
转载地址:http://rfcgi.baihongyu.com/