android系统相机相册调用封装

版权声明: https://blog.csdn.net/jjj11223344/article/details/79961468
public abstract class PickPhotoActivity extends BaseActivity {
    private static final String KEY_PHOTO_PATH = "photo_path";
    private static final int TAKE_PHOTO_LARGR = 1000;
    private static final int PICK_PHOTO_ALBUM = 1001;
    private static final int TAKE_PHOTO_LARGR_CROP = 1002;
    private static final int PICK_PHOTO_ALBUM_CROP = 1003;
    private static final int CROP_PHOTO = 1004;

    private String mSavePhotoPath = null;

    //调用Camera拍照时,此Activity很容易因为内存问题,被系统Kill掉,因此需要保存拍照的路径
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (mSavePhotoPath != null) {
            outState.putString(KEY_PHOTO_PATH, mSavePhotoPath);
        }
        super.onSaveInstanceState(outState);
    }

    //获取之前保存的拍照的路径 
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        mSavePhotoPath = savedInstanceState.getString(KEY_PHOTO_PATH);
        super.onRestoreInstanceState(savedInstanceState);
    }

    /**
     * @param needCapture true 拍照; false 相册
     * @param needCrop    true 裁剪
     */
    public void pickPhoto(final boolean needCapture, final boolean needCrop) {
        使用RxPermission进行权限判断
         PermissionsUtil.request(this, new PermissionsUtil.OnPermission() {
                    @Override
                    public void onPermissionGranted(Context context) {
                        if (needCapture) {
                            String filePath = Environment.getExternalStorageDirectory() + "/DCIM/Camera/";
                            File file = new File(filePath);
                            if (!file.exists()) {
                                file.mkdir();
                            }
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            mSavePhotoPath = filePath + ImagePicker.imageName();
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
                                    .fromFile(new File(mSavePhotoPath)));
                            startActivityForResult(intent, needCrop ? TAKE_PHOTO_LARGR_CROP : TAKE_PHOTO_LARGR);

                        } else {
                            Intent intent = new Intent(Intent.ACTION_PICK);
                            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                    "image/*");
                            startActivityForResult(intent, needCrop ? PICK_PHOTO_ALBUM_CROP : PICK_PHOTO_ALBUM);
                        }
                    }

                    @Override
                    public void onPermissionCancel(Context context) {

                    }
                },
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == TAKE_PHOTO_LARGR) {// 拍照, 不剪裁
                if (mSavePhotoPath != null) {
                    onPhotoPick(mSavePhotoPath);
                }
            }
            if (requestCode == PICK_PHOTO_ALBUM) {// 相册,不剪裁
                if (data != null) {
                    String filepath = ImagePicker.getFilepath(this, data.getData());
                    LOG.d("TAG", "Uri path = " + filepath);
                    onPhotoPick(filepath);
                }
            }
            if (requestCode == TAKE_PHOTO_LARGR_CROP) {// 拍照, 需要剪裁
                if (mSavePhotoPath != null) {
                    File file = new File(mSavePhotoPath);
                    Intent intent = ImagePicker.cropRawPhoto(this, Uri.fromFile(file));
                    startActivityForResult(intent, CROP_PHOTO);
                }
            }
            if (requestCode == PICK_PHOTO_ALBUM_CROP) {// 相册, 需要剪裁
                if (data != null) {
                    Uri uri = data.getData();
                    Intent intent = ImagePicker.cropRawPhoto(this, uri);
                    startActivityForResult(intent, CROP_PHOTO);
                }
            }
            if (requestCode == CROP_PHOTO) {
                if (data != null) {
//                    Bitmap bitmap = BitmapFactory.decodeFile(App.getPhotoCachePath() + "temp.jpg");
//                    onPhotoCrop(bitmap);
                    Bundle extras = data.getExtras();
                    if (extras != null) {
                        Bitmap bitmap = extras.getParcelable("data");
                        onPhotoCrop(bitmap);
                    }
                }
            }
        }
    }

    public void onPhotoPick(String filepath) {
        LOG.d("TAG", "onPhotoPick path = " + filepath);//在子类中获取照片路径
    }

    public void onPhotoCrop(Bitmap bitmap) {
        LOG.d("TAG", "onPhotoCrop bitmap = " + bitmap);//在子类中获取裁剪后照片路径
    }
}

public class ImagePicker {
	// 裁剪后图片的宽(X)和高(Y)。
	private static int output_X = 162;
	private static int output_Y = 161;

	/**
	 * 检测sd是否可用
	 * @return
	 */
	public static boolean sdExits() {
		String sdStatus = Environment.getExternalStorageState();
		if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { //
			return false;
		}
		return true;
	}

	/**
	 * 裁剪原始的图片
	 * @return
	 */
	public static Intent cropRawPhoto(Context context, Uri uri) {

		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 设置裁剪
		intent.putExtra("crop", "true");
		// aspectX , aspectY :宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX , outputY : 裁剪图片宽高
		intent.putExtra("outputX", output_X);
		intent.putExtra("outputY", output_Y);
		intent.putExtra("return-data", true);
		return intent;
	}

	/**
	 * 使用系统当前时间给图片命名
	 * @return
	 */
	public static String imageName() {
		new DateFormat();
		String name = DateFormat.format("yyyyMMdd_hhmmss",
				Calendar.getInstance(Locale.CHINA))
				+ ".jpg";
		return name;
	}

	public static String getFilepath(Context context, Uri uri) {
		String filepath = null;
		try {
			String[] proj = { MediaStore.Images.Media.DATA };
			Cursor cursor = ((Activity) context).getContentResolver().query(
					uri, proj, null, null, null);
			int column_index = cursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			cursor.moveToFirst();
			filepath = cursor.getString(column_index);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return filepath;
	}
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页