裁剪保存位图

时间:2013-01-23 21:51:47

标签: android android-camera

我正在尝试拍摄照片,然后裁剪它。所以基本上,我启动相机Intent,然后我开始裁剪意图。

由于我无法通过“onActivityResult”数据传递大位图,因此我创建了一个URI以将图像直接保存到SDCard,但由于某种原因,它被保存在模拟器上,但不保存在真实设备上。 (我的意思是,CROP不会保存在真实设备上。)

这是代码:

File resultingFile;
Bitmap fotoCreada;
Uri uriFoto;

相机意图:

File folder = new File(Environment.getExternalStorageDirectory().toString()+"/PictureFolder/");
folder.mkdirs();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
resultingFile=new File(folder.toString() + "/"+currentDateandTime+".jpg");
this.uriFoto=Uri.fromFile(resultingFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, this.uriFoto);
startActivityForResult(cameraIntent, 1888); 

然后我检索结果:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Si venim de la camera
    if( requestCode == 1888 && resultCode == -1) { //-1 = TOT HA ANAT BE.
        Bitmap photo = BitmapFactory.decodeFile(resultingFile.getPath());
        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        matrix.postScale(0.5f,0.5f);
        Bitmap photoRotated = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
        //Desem foto rotada.
        this.fotoCreada=photoRotated;
        try {
            FileOutputStream out = new FileOutputStream(this.resultingFile.getPath());
            this.fotoCreada.compress(Bitmap.CompressFormat.PNG, 90, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
        performCrop();
    }

所以我调用performCrop();

private void performCrop() {
    try {
        Log.d("debugging","Estic a crop. La foto és:"+this.uriFoto);
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(this.uriFoto, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        cropIntent.putExtra("outputX", 600);
        cropIntent.putExtra("outputY", 600);
        //cropIntent.putExtra("return-data", true);
        cropIntent.putExtra("ouput", this.uriFoto);
        startActivityForResult(cropIntent,1234);
    }
    catch(ActivityNotFoundException anfe){
        //display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this.cont, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

如你所见:

cropIntent.putExtra("ouput", this.uriFoto);

基本上告诉将裁剪输出到该URI。 URI是正确的,因为我已经调试它(LogCat):

  

Estic a crop。你的位置:文件:///mnt/sdcard/PictureFolder/20130123_223058.jpg

然后,我回到活动结果:

[... previous onactivityresult]
if (requestCode == 1234 && resultCode == -1){
        Log.d("debugging","Ha acabat el croop.");
        Uri imageUri = this.uriFoto;
        try {
            Bitmap photo = MediaStore.Images.Media.getBitmap(this.cont.getContentResolver(), imageUri);
            this.fotoCreada=photo;
            ((ImageView) myFragmentView.findViewById(R.id.fotoCapturada)).setImageBitmap(this.fotoCreada);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //Bitmap photo = BitmapFactory.decodeFile(resultingFile.getPath());     
    }

但我最终得到的,是显示(setImageBitmap(this.fotoCreada);)从相机捕获的图像,但不是裁剪的图片。这发生在真实设备上,但不在模拟器上发生!

我试着用最好的方式解释。随意问。

感谢。

0 个答案:

没有答案