Android中简单实现选择图片并裁剪

在android中选择图片是一个很常见的功能,图片的来源通常情况下是从相机获取和从相册获取两种。
先来写一个简单的选择按钮和一个能显示图片的ImageView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bt_picture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取图片" />

    <ImageView
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

LinearLayout>

接下来就是实现的逻辑

    private String[] takePhotoitems;// 获取照片的操纵选项

    public static final int TAKE_PHOTO = 1;

    public static final int CROP_PHOTO = 2;

    private Button getPicture;

    private ImageView picture;

    private Uri imageUri;

设置按钮的点击事件

@Override
    public void onClick(View v) {
        takePhotoitems = new String[] { "选择本地图片", "拍照" };
        imageUri = getImageUri();
        new AlertDialog.Builder(this)
                .setTitle("设置头像")
                .setItems(takePhotoitems,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                switch (which) {
                                case 0:

                                    getImageUri();
                                    Intent intent = new Intent(
                                            Intent.ACTION_GET_CONTENT);
                                    intent.setDataAndType(
                                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                            "image/*");
                                    intent.setType("image/*");
                                    intent.putExtra("crop", "true");

                                    intent.putExtra("scale", true);
                                    intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                            imageUri);
                                    startActivityForResult(intent, CROP_PHOTO);
                                    break;

                                case 1:
                                    // 拍照

                                    getImageUri();
                                    if (isHasSdcard()) {
                                        Intent takePhotoIntent = new Intent(
                                                MediaStore.ACTION_IMAGE_CAPTURE);
                                        takePhotoIntent.putExtra(
                                                MediaStore.EXTRA_OUTPUT,
                                                imageUri);
                                        startActivityForResult(takePhotoIntent,
                                                TAKE_PHOTO);
                                    } else {
                                        Toast.makeText(MainActivity.this,
                                                "未检测到sd卡", Toast.LENGTH_LONG)
                                                .show();
                                    }
                                    break;
                                }
                            }
                        })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();

    }

用到的两个工具方法

    /**
     * 获取图片的路径
     */
    private Uri getImageUri() {
        File outputImage = new File(Environment.getExternalStorageDirectory(),
                "tempImage.jpg");
        if (outputImage.exists()) {
            outputImage.delete();
        }
        try {
            outputImage.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Uri.fromFile(outputImage);
    };
    /**
     * 判断sd卡是否存在
     */
    public static boolean isHasSdcard() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

获取到图片后进行裁剪

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case TAKE_PHOTO:
            if (resultCode == RESULT_OK) {
                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setDataAndType(imageUri, "image/*");
                intent.putExtra("scale", true);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, CROP_PHOTO);
            }
            break;
        case CROP_PHOTO:
            if (resultCode == RESULT_OK) {
                try {
                    Bitmap bitmap = BitmapFactory
                            .decodeStream(getContentResolver().openInputStream(
                                    imageUri));
                    picture.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            break;
        default:
            break;
        }
    }

你可能感兴趣的:(android)