public void showImagePickDialog() { String title = "获取图片方式"; String[] choices = new String[]{"拍照", "从手机中选择"}; new AlertDialog.Builder( this) .setTitle(title) .setItems(choices, new DialogInterface.OnClickListener() { .setNegativeButton("返回", null) .show(); }
首先show出来消息对话框,其中有两个选项,拍照获取或者从手机相册
然后我们实现对点击事件进行监听
public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); switch (which) { case 0: ImageUtils.openCameraImage(MainActivity.this); break; case 1: ImageUtils.openLocalImage(MainActivity.this); break; } }
当获取到点击事件后获取相应的方法,openCameraImage/openLocalImage;入伙时选择拍照我们要先确定内存情况,并且设置存储路径
private static Uri createImagePathUri(Context context) { Uri imageFilePath = null; String status = Environment.getExternalStorageState(); SimpleDateFormat timeFormatter = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA); long time = System.currentTimeMillis(); String imageName = timeFormatter.format(new Date(time)); ContentValues values = new ContentValues(3); values.put(MediaStore.Images.Media.DISPLAY_NAME, imageName); values.put(MediaStore.Images.Media.DATE_TAKEN, time); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡,优先使用SD卡存储,当没有SD卡时使用手机存储 imageFilePath = context.getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } else { imageFilePath = context.getContentResolver().insert( MediaStore.Images.Media.INTERNAL_CONTENT_URI, values); } Log.i("", "生成的照片输出路径:" + imageFilePath.toString()); return imageFilePath; } }
public static void openCameraImage(final Activity activity) { ImageUtils.imageUriFromCamera = ImageUtils.createImagePathUri(activity); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, ImageUtils.imageUriFromCamera); activity.startActivityForResult(intent, ImageUtils.GET_IMAGE_BY_CAMERA); }
MediaStore.EXTRA_OUTPUT参数不设置时,系统会自动生成一个uri,但是只会返回一个缩略图
在onActivityResult中获取图片数据。
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_CANCELED) { return; } switch (requestCode) { case ImageUtils.GET_IMAGE_BY_CAMERA: if(ImageUtils.imageUriFromCamera != null) { ImageUtils.cropImage(this, ImageUtils.imageUriFromCamera); break; } break;
拍照获取图片并且自定义Uri(ImageUtils.imageUriFromCamera),用于保存拍照后图片地址,最后通过cropImage进行裁剪;
1.宽高和比例都不设置时,裁剪框可以自行调整(比例和大小都可以随意调整)
2.只设置裁剪框宽高比(aspect)后,裁剪框比例固定不可调整,只能调整大小
3.裁剪后生成图片宽高(output)的设置和裁剪框无关,只决定最终生成图片大小
4.裁剪框宽高比例(aspect)可以和裁剪后生成图片比例(output)不同,此时,
public static void cropImage(Activity activity, Uri srcUri) { ImageUtils.cropImageUri = ImageUtils.createImagePathUri(activity); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(srcUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra(MediaStore.EXTRA_OUTPUT, ImageUtils.cropImageUri); intent.putExtra("return-data", false); activity.startActivityForResult(intent, CROP_IMAGE); }最后是全部代码
public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); findViewById(R.id.btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showImagePickDialog(); } }); } public void showImagePickDialog() { String title = "获取图片方式"; String[] choices = new String[]{"拍照", "从手机中选择"}; new AlertDialog.Builder( this) .setTitle(title) .setItems(choices, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); switch (which) { case 0: ImageUtils.openCameraImage(MainActivity.this); break; case 1: ImageUtils.openLocalImage(MainActivity.this); break; } } }) .setNegativeButton("返回", null) .show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_CANCELED) { return; } switch (requestCode) { case ImageUtils.GET_IMAGE_BY_CAMERA: if(ImageUtils.imageUriFromCamera != null) { ImageUtils.cropImage(this, ImageUtils.imageUriFromCamera); break; } break; case ImageUtils.GET_IMAGE_FROM_PHONE: if(data != null && data.getData() != null) { ImageUtils.cropImage(this, data.getData()); } break; case ImageUtils.CROP_IMAGE: if(ImageUtils.cropImageUri != null) { iv.setImageURI(ImageUtils.cropImageUri); } break; default: break; } } }第二部分
public class ImageUtils { public static final int GET_IMAGE_BY_CAMERA = 5001; public static final int GET_IMAGE_FROM_PHONE = 5002; public static final int CROP_IMAGE = 5003; public static Uri imageUriFromCamera; public static Uri cropImageUri; public static void openCameraImage(final Activity activity) { ImageUtils.imageUriFromCamera = ImageUtils.createImagePathUri(activity); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, ImageUtils.imageUriFromCamera); activity.startActivityForResult(intent, ImageUtils.GET_IMAGE_BY_CAMERA); } public static void openLocalImage(final Activity activity) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); activity.startActivityForResult(intent, ImageUtils.GET_IMAGE_FROM_PHONE); } public static void cropImage(Activity activity, Uri srcUri) { ImageUtils.cropImageUri = ImageUtils.createImagePathUri(activity); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(srcUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra(MediaStore.EXTRA_OUTPUT, ImageUtils.cropImageUri); intent.putExtra("return-data", false); activity.startActivityForResult(intent, CROP_IMAGE); } private static Uri createImagePathUri(Context context) { Uri imageFilePath = null; String status = Environment.getExternalStorageState(); SimpleDateFormat timeFormatter = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA); long time = System.currentTimeMillis(); String imageName = timeFormatter.format(new Date(time)); ContentValues values = new ContentValues(3); values.put(MediaStore.Images.Media.DISPLAY_NAME, imageName); values.put(MediaStore.Images.Media.DATE_TAKEN, time); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡,优先使用SD卡存储,当没有SD卡时使用手机存储 imageFilePath = context.getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } else { imageFilePath = context.getContentResolver().insert( MediaStore.Images.Media.INTERNAL_CONTENT_URI, values); } Log.i("", "生成的照片输出路径:" + imageFilePath.toString()); return imageFilePath; } }布局页面,只有两个view,简单的写了一下,仅供参考
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:background="@android:color/white" tools:context=".MainActivity" > <Button android:id="@+id/btn" android:text="获取照片" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <ImageView android:id="@+id/iv" android:background="@android:color/black" android:layout_height="wrap_content" android:layout_width="wrap_content" /> </LinearLayout>