[Android Pro] 调用系统相机和图库,裁剪图片

    private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
    private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
    private static final int PHOTO_REQUEST_CUT = 3;// 结果
    private File tempFile = new File(Environment.getExternalStorageDirectory(),
            getPhotoFileName());

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case PHOTO_REQUEST_TAKEPHOTO:// 当选择拍照时调用
            startPhotoZoom(Uri.fromFile(tempFile));
            break;
        case PHOTO_REQUEST_GALLERY:// 当选择从本地获取图片时
            // 做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同
            if (data != null) {
                System.out.println("11================");
                startPhotoZoom(data.getData());
            } else {
                System.out.println("================");
            }
            break;
        case PHOTO_REQUEST_CUT:// 返回的结果
            if (data != null)
                // setPicToView(data);
                sentPicToNext(data);
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
 // 使用系统当前日期加以调整作为照片的名称
    private String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "'IMG'_yyyyMMdd_HHmmss");
        return dateFormat.format(date) + ".jpg";
    }

 

调用系统拍照功能: 

     

Intent cameraintent = new Intent(
                                    MediaStore.ACTION_IMAGE_CAPTURE);
                            // 指定调用相机拍照后照片的储存路径
                            cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,
                                    Uri.fromFile(tempFile));
                            startActivityForResult(cameraintent,
                                    PHOTO_REQUEST_TAKEPHOTO);

 

调用系统相册功能: 

 

Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
                            getAlbum.setType("image/*");
                            startActivityForResult(getAlbum, PHOTO_REQUEST_GALLERY);

 

调用系统裁剪功能: 

 

private void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // crop为true是设置在开启的intent中设置显示的view可以剪裁
        intent.putExtra("crop", "true");

        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        // outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        intent.putExtra("noFaceDetection", true);
        System.out.println("22================");
        startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

 

自定义对话框: 

        mDialog = new AlertDialog.Builder(this, R.style.FullScreenDialog)
                .create();
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
            mDialog.setContentView(R.layout.dialog_select_imge);
            mDialog.setCanceledOnTouchOutside(false);
}

自定义style :

<style name="FullScreenDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">false</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
    </style>

 

posted @ 2014-12-13 23:49  demoblog  阅读(16380)  评论(0编辑  收藏  举报