android打开相册的intent,Android 调用相机、打开相册、裁剪图片

private ImageView iv_user_photo;

private String fileName = "";

private File tempFile;

private int crop = 300;// 裁剪大小

private static final int OPEN_CAMERA_CODE = 10;

private static final int OPEN_GALLERY_CODE = 11;

private static final int CROP_PHOTO_CODE = 12;

private OnClickListener PopupWindowItemOnClick = new OnClickListener() {

@Override

public void onClick(View v) {

menuWindow.dismiss();

switch (v.getId()) {

// 拍照

case R.id.btn_camera:

initFile();

openCamera();

break;

// 相册

case R.id.btn_gallery:

initFile();

openGallery();

break;

default:

break;

}

}

};

public void initFile() {

if(fileName.equals("")) {

if(FileUtil.existSDCard()) {

String path = Environment.getExternalStorageDirectory() + File.separator + "JanuBookingOnline" + File.separator;

FileUtil.mkdir(path);

Logger.i("path:" + path);

fileName = path + "user_head_photo.jpg";

tempFile = new File(fileName);

} else {

CommonUitl.toast(context, "请插入SD卡");

}

}

}

/**

* 调用相机

*/

public void openCamera() {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 打开相机

intent.putExtra("output", Uri.fromFile(tempFile));

startActivityForResult(intent, OPEN_CAMERA_CODE);

}

/**

* 打开相册

*/

public void openGallery() {

Intent intent = new Intent(Intent.ACTION_PICK);// 打开相册

intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");

intent.putExtra("output", Uri.fromFile(tempFile));

startActivityForResult(intent, OPEN_GALLERY_CODE);

}

/**

* 裁剪图片

* @param uri

*/

public void cropPhoto(Uri uri) {

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(uri, "image/*");

intent.putExtra("output", Uri.fromFile(tempFile));

intent.putExtra("crop", true);

intent.putExtra("aspectX", 1);

intent.putExtra("aspectY", 1);

intent.putExtra("outputX", crop);

intent.putExtra("outputY", crop);

startActivityForResult(intent, CROP_PHOTO_CODE);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == 1)

return;

switch (requestCode) {

case OPEN_CAMERA_CODE:

cropPhoto(Uri.fromFile(tempFile));

break;

case OPEN_GALLERY_CODE:

cropPhoto(data.getData());

break;

case CROP_PHOTO_CODE:

try {

BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = 2;

Bitmap bitmap = BitmapFactory.decodeFile(fileName, options);

if (bitmap != null) {

iv_user_photo.setImageBitmap(bitmap);

CommonUitl.sharedPreferences(context, AppConstants.USER_PHOTO, fileName);

}

} catch (Exception e) {

e.printStackTrace();

}

break;

default:

break;

}

super.onActivityResult(requestCode, resultCode, data);

}

20d183758a2bfb3dea89be135bd9d53a.png

7207861b6f36bdd3f3dbd6ffe6c4b8fe.png

7727c262d87befc4a9b965d3fc3e1a6d.png

来源:https://www.cnblogs.com/chaoyaximo/p/3307538.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值