调用相册拍照裁剪后返回

来源:互联网 发布:java是什么专业 编辑:程序博客网 时间:2024/06/05 02:09
//相册tv_editIcon_xiangce.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent("android.intent.action.PICK");                intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");                intent.putExtra("output", Uri.fromFile(sdcardTempFile));                intent.putExtra("crop", "true");                intent.putExtra("aspectX", 3);// 裁剪框比例                intent.putExtra("aspectY", 4);                intent.putExtra("outputX", 120);// 输出图片大小                intent.putExtra("outputY", 160);                startActivityForResult(intent, 100);}});}


//拍照tv_editIcon_paizhao.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                // 指定调用相机拍照后照片的储存路径                cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(tempFile));                startActivityForResult(cameraintent, 101);}});




在调用返回后的处理:

    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {            if (requestCode == 100) {//            Drawable draw = Drawable.createFromPath(sdcardTempFile.getAbsolutePath());                    Bitmap bmp = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath());//                    imageView.setImageBitmap(bmp);                                        iv_editIcon_icon.setBackgroundDrawable(new BitmapDrawable(bmp));                    currentIcon = sdcardTempFile.getAbsolutePath();            }else if(requestCode == 101){//选择拍照           startPhotoZoom(Uri.fromFile(tempFile));            }else if(requestCode == 102){            if (data != null)            // setPicToView(data);            sentPicToNext(data);                        }    }        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", 3);        intent.putExtra("aspectY", 4);        // outputX,outputY 是剪裁图片的宽高        intent.putExtra("outputX", 60);        intent.putExtra("outputY", 80);        intent.putExtra("return-data", true);        intent.putExtra("noFaceDetection", true);        startActivityForResult(intent, 102);     }     // 将进行剪裁后的图片传递到下一个界面上    private void sentPicToNext(Intent picdata) {        Bundle bundle = picdata.getExtras();        if (bundle != null) {            Bitmap photo = bundle.getParcelable("data");            if (photo==null) {            }else {            iv_editIcon_icon.setBackgroundDrawable(new BitmapDrawable(photo));                        File myCaptureFile = new File(Environment.getExternalStorageDirectory(),fileName);            try {            saveMyBitmap(photo,myCaptureFile);currentIcon = myCaptureFile.getAbsolutePath();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}            }        }    }    //保存图片到本地    public void saveMyBitmap(Bitmap bm,File file) throws IOException {    file.createNewFile();        FileOutputStream fOut = null;        try {                fOut = new FileOutputStream(file);        } catch (FileNotFoundException e) {                e.printStackTrace();        }        bm.compress(Bitmap.CompressFormat.JPEG, 100, fOut);        try {                fOut.flush();        } catch (IOException e) {                e.printStackTrace();        }        try {                fOut.close();        } catch (IOException e) {                e.printStackTrace();        }    }


0 0
原创粉丝点击