public class ThirdActivity extends Activity{
Button take,photo;
ImageView image;
private Uri photoUri = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
photo = (Button)findViewById(R.id.third_button1);
take = (Button)findViewById(R.id.third_button2);
image = (ImageView)findViewById(R.id.third_imageView1);
photo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 系统相册
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 调用android的图库
startActivityForResult(i, 2);
}
});
take.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 系统相机
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);// 调用android自带的照相机
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy_MM_dd_HH_mm_ss");
String filename = timeStampFormat
.format(new Date());
**ContentValues values = new ContentValues();
values.put(Media.TITLE, filename);**
**photoUri = getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);**
**intent.putExtra(MediaStore.EXTRA_OUTPUT,
photoUri);**
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
//相机
if (resultCode == RESULT_OK) {
if (data != null && data.getData() != null) {
Uri uri = data.getData();
photoUri = uri;
}
startImageAction(photoUri, 200, 200, 3, true);
}
break;
case 2:
//相册
if (resultCode == RESULT_OK) {
if (data != null && data.getData() != null) {
Uri uri = data.getData();
photoUri = uri;
System.out.println(">>>>>>>>>>>>>>>>>>>>>> " + photoUri.toString());
}
startImageAction(photoUri, 200, 200, 3, true);
}
break;
case 3:
if (data == null) {
return;
} else {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap bitmap = extras.getParcelable("data");
if (bitmap != null) {
image.setImageBitmap(bitmap);
if (bitmap != null && bitmap.isRecycled()) {
bitmap.recycle();
}
}
}
}
break;
case Activity.RESULT_CANCELED:
break;
}
}
private void startImageAction(Uri uri, int outputX, int outputY,
int requestCode, boolean isCrop) {
Intent intent = null;
if (isCrop) {
intent = new Intent("com.android.camera.action.CROP");
} else {
//intent = new Intent(Intent.ACTION_GET_CONTENT, null);
}
//在这个Uri下请求什么类型的数据
intent.setDataAndType(uri, "image/*");
//裁剪照片的信号
intent.putExtra("crop", "true");
// 剪切的宽高比为1:2
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);
//是否返回数据
intent.putExtra("return-data", true);
// intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//关闭人脸检测
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, requestCode);
}
}
Android 调用相机或图库,裁剪图片
最新推荐文章于 2021-10-23 22:13:38 发布