android调用系统拍照、从系统相册中选择照片并剪裁

本文介绍了一种在Android应用中实现拍照及从相册选取图片的方法,并提供了如何处理这些图片,包括图片裁剪和保存的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

拍照 

 //拍照
    private void takephoto(){

 //定一个拍照图片的路径
        photopath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/lmp/pic/"
                + LmpUntil.gettime() + ".png";

        if (Build.VERSION.SDK_INT >= 24 ) {

            //兼容android7.0 使用共享文件的形式
            ContentValues contentValues = new ContentValues(1);
            contentValues.put(MediaStore.Images.Media.DATA, photopath);

  //定一个Uri

            output = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

        }else{

            output = Uri.fromFile(new File(photopath));
        }

        Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, output);
        startActivityForResult(openCameraIntent, TAKE_PHOTO);
    }

从系统相册中选择

                //调用系统图片选择
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, 33 );
onActivityResult中的操作
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == TAKE_PHOTO && resultCode == RESULT_OK) {

//注释部分是直接获取拍照后的bitmap

//            Bitmap bitmap = null;
//            if (data != null) {
//                if (data.hasExtra("data")) {
//                    Log.i("URI", "data is not null");
//                    bitmap = data.getParcelableExtra("data");
//                    headphoto.setImageBitmap(bitmap);//imageView即为当前页面需要展示照片的控件,可替换
//                }
//            } else {
//                Log.i("URI", "Data is null");
//                if (Build.VERSION.SDK_INT >= 24){
//
//                    try {
//                        bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(output));
//                    } catch (FileNotFoundException e) {
//                        e.printStackTrace();
//                    }
//                    headphoto.setImageBitmap(bitmap);
//                }else {
//                    bitmap = BitmapFactory.decodeFile(photopath);
//                    headphoto.setImageBitmap(bitmap);
//                }
//            }


            //剪切
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(output, "image/*");
            intent.putExtra("scale", true);

            intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
            startActivityForResult(intent, CROP_PHOTO); // 启动裁剪程序

        }else if (requestCode == CROP_PHOTO&& resultCode == RESULT_OK) {


//裁剪完成将bitmap保存至目标位置
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
                        .openInputStream(output));
                headphoto.setImageBitmap(bitmap);

                File fileorg = new File(photopath);
                if(fileorg.exists()){
                    fileorg.delete();
                }

                photocrappath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/lmp/pic/headcrap.png";
                File file = new File(photocrappath);
                FileOutputStream out = null;
                out = new FileOutputStream(file);
                if (bitmap != null) {
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                } else {
                    throw new FileNotFoundException();
                }
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if ( requestCode == 33 ){ //系统图片选择之后的回调

            if (data != null) {
                Uri uri = data.getData();
                try {

                    Bitmap mBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
//                    Bitmap mBitmap = MediaStore.Images.Media.getBitmap(cr, uri);//显得到bitmap图片

                    headphoto.setImageBitmap(mBitmap);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

剪裁后可以根据uri获取文件路径 拿到路径后可以做个压缩处理下面提供根据uri获取文件路径的方法 

// 从Uri中获取文件路径
    public static String getpathfromuri( Uri uri ,Context context){

        final String scheme = uri.getScheme();
        String lujing = null;
        if ( scheme == null )
            lujing = uri.getPath();
        else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
            lujing = uri.getPath();
        } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
            Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
            if ( null != cursor ) {
                if ( cursor.moveToFirst() ) {
                    int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
                    if ( index > -1 ) {
                        lujing = cursor.getString( index );
                    }
                }
                cursor.close();
            }
        }
        return lujing;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值