当前位置:   article > 正文

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

android intent 打开图库

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/175245
推荐阅读
相关标签
  

闽ICP备14008679号