2019独角兽企业重金招聘Python工程师标准>>>
方法一
判断是否有SD卡
public static boolean hasSDCard() {
return Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState());
}
选择图库路径
if (hasSDCard()) {
photoAlbum = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else {
photoAlbum = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
}
选择图片
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(kaibesimApplication.getPhotoAlbum(), "image/*");
startActivityForResult(intent, requestCodeChangePhoto);
剪裁图片,返回的结果里
uri = data.getData();
startImageAction(uri, 200, 200, requestCodeGetContent);
private void startImageAction(Uri uri, int outputX, int outputY,
int requestCode) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("scale", true);
// intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(Intent.createChooser(intent, "选择图片"), requestCode);
}
方法二
这个方法比较简单,使用的是Intent.ACTION_GET_CONTENT
startImageAction(200, 200, requestCodeGetContent);
private void startImageAction(int outputX, int outputY, int requestCode) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("scale", true);
// intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, requestCode);
}