相机,图库 新

  /** 图片路径 **/
    private String mCurrentPhotoStr;
    private Bitmap mPhotoImage;
    private static final int TAKE_PHOTO = 1;
    private static final int GET_PHOTO = 2;


  //相机拍照
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent, TAKE_PHOTO);

 //图库选取
                Intent intent2 = new Intent(Intent.ACTION_PICK, null);
                intent2.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*" );
                startActivityForResult(intent2, GET_PHOTO);

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        getActivity();

        if (requestCode == TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
            mImagevFront.setImageBitmap(bitmap);        // 将图片显示在ImageView里
        }
        if (requestCode == GET_PHOTO && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();
            // cursor游标
            Cursor cursor = getActivity().getContentResolver().query(uri, null, null,
                    null, null);
            cursor.moveToFirst();
            int idx = cursor
                    .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            mCurrentPhotoStr = cursor.getString(idx);
            cursor.close();
            mPhotoImage = BitmapFactory.decodeFile(mCurrentPhotoStr);
            mImagevFront.setImageBitmap(mPhotoImage);
        }
    }

  private static final int TAKE_PHOTO_FRONT = 1;
    private static final int TAKE_PHOTO_BACK = 3;
    private static final int GET_PHOTO_FRONT= 2;
    private static final int GET_PHOTO_BCAK= 4;

 //相机拍照
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File outputImage = new File(Environment.getExternalStorageDirectory(),"tempImage.jpg");
                if(outputImage.exists()) {
                    outputImage.delete();
                    try {
                        outputImage.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                Uri imageUri = Uri.fromFile(outputImage);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                Log.e("uri", imageUri.toString());
                switch (view.getId()) {
                    case R.id.imagev_front:
                        startActivityForResult(intent, TAKE_PHOTO_FRONT);
                        break;
                    case R.id.imagev_back:
                        startActivityForResult(intent, TAKE_PHOTO_BACK);
                }

            }

            @Override
            public void OnMiddleClick() {
                //图库选取
                Intent intent2 = new Intent(Intent.ACTION_PICK, null);
                intent2.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                switch (view.getId()) {
                    case R.id.imagev_front:
                        startActivityForResult(intent2, GET_PHOTO_FRONT);
                        break;
                    case R.id.imagev_back:
                        startActivityForResult(intent2, GET_PHOTO_BCAK);;
                }

            }

            @Override
            public void OnBottomClick() {
                myPopview.dismiss();
            }
        });
    }

        public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        getActivity();

        if (requestCode == TAKE_PHOTO_FRONT && resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            //Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2; // 直接设置它的压缩率,表示1/2
            Bitmap bitmap =  BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/tempImage.jpg", options);
            mImagevFront.setImageBitmap(bitmap);        // 将图片显示在ImageView里
        }
        if (requestCode == GET_PHOTO_FRONT && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();
            // cursor游标
            Cursor cursor = getActivity().getContentResolver().query(uri, null, null,
                    null, null);
            cursor.moveToFirst();
            int idx = cursor
                    .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            mCurrentPhotoStr = cursor.getString(idx);
            cursor.close();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2; // 直接设置它的压缩率,表示1/2
            mPhotoImage = BitmapFactory.decodeFile(mCurrentPhotoStr,options);
            mImagevFront.setImageBitmap(mPhotoImage);
        }

            if (requestCode == TAKE_PHOTO_BACK && resultCode == Activity.RESULT_OK) {
                Bundle bundle = data.getExtras();
                //Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2; // 直接设置它的压缩率,表示1/2
                Bitmap bitmap =  BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/tempImage.jpg", options);
                mImagevBack.setImageBitmap(bitmap);        // 将图片显示在ImageView里
            }
            if (requestCode == GET_PHOTO_BCAK && resultCode == Activity.RESULT_OK) {
                Uri uri = data.getData();
                // cursor游标
                Cursor cursor = getActivity().getContentResolver().query(uri, null, null,
                        null, null);
                cursor.moveToFirst();
                int idx = cursor
                        .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                mCurrentPhotoStr = cursor.getString(idx);
                cursor.close();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2; // 直接设置它的压缩率,表示1/2
                mPhotoImage = BitmapFactory.decodeFile(mCurrentPhotoStr,options);
                mImagevBack.setImageBitmap(mPhotoImage);
            }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值