android系统自带的有裁剪图片功能,在应用过程中,碰到了诸多问题,在些做个记录:
代码如下,注释很详细,记得加写入sd卡数据的权限
public class MainActivity extends Activity {
private static final int TAKE_BIG_PICTURE = 100;
private static final int TAKE_SMALL_PICTURE = 101;
private static final int CROP_BIG_PICTURE = 102;
private static final int CROP_SMALL_PICTURE = 103;
private Button pick_photo, take_photo;
private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";
Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pick_photo = (Button) findViewById(R.id.bt_gallery);
take_photo = (Button) findViewById(R.id.bt_camera);
imageView = (ImageView) findViewById(R.id.iv_photo);
pick_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//启动图片库
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, TAKE_SMALL_PICTURE);
}
});
take_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//启动相机去拍照,直接传入了uri,照完以后,会把照片直接存储到这个uri
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_BIG_PICTURE);//or TAKE_SMALL_PICTURE
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_BIG_PICTURE:
//照相机照完以后返回结果,因为在启动相机的时候,已经指定了uri,所以可以直接从这个uri取
//调用裁剪方法(注意,这里没有判断是否为空,应用中应该判断)
cropImageUri(imageUri, 800, 400, CROP_BIG_PICTURE);
break;
case TAKE_SMALL_PICTURE:
//调用图片库在这里返回结果
//调用裁剪方法(注意,这里没有判断是否为空,应用中应该判断)
cropImageUri(data.getData(), 300, 150, CROP_SMALL_PICTURE);
break;
case CROP_BIG_PICTURE:
//这里是相机裁剪后的返回
//可以参照裁剪方法,里面已经指定了uri,所以在这里,直接可以从里面取uri,然后获取bitmap,并且设置到imageview
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
break;
case CROP_SMALL_PICTURE:
//这里是调用图片库裁剪后的返回
//可以参照裁剪方法,里面已经指定了uri,所以在这里,直接可以从里面取uri,然后获取bitmap,并且设置到imageview
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void cropImageUri(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");
//设置xy的裁剪比例
intent.putExtra("aspectX", 2);
intent.putExtra("aspectY", 1);
//设置输出的宽高
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
//是否缩放
intent.putExtra("scale", false);
//输入图片的Uri,指定以后,可以在这个uri获得图片
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//是否返回图片数据,可以不用,直接用uri就可以了
intent.putExtra("return-data", false);
//设置输入图片格式
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//是否关闭面部识别
intent.putExtra("noFaceDetection", true); // no face detection
//启动
startActivityForResult(intent, requestCode);
}
}
开发过程中遇到的问题:
1、我还看到网上有一些其它写法,选择图片库,直接返回图片数据。那样太占内存,不如直接返回uri,用的时候,再去取图片。
2、Android java.io.FileNotFoundException: open failed: EACCES (Permission denie
华为手机,老是取不到图片,读log,是因为权限问题,我们浪费了很长时间,最后查明,是手机连在电脑上处于调用状态的原因,
有些手机,连上电脑以后,sd卡是不可读取的,所以才会出现权限问题,这个大家可以注意一下