裁剪图像并保存好,但imageview有旧图像,为什么?

时间:2015-04-28 17:27:51

标签: android camera crop

我在相机拍摄后对图像进行裁剪 - 一切正常。 图片出现在我的画廊都裁剪和可爱 - 所有工作。 我使用uri调用crop将图像分配到imageview - 不工作

继承我的代码,我认为我的问题是uri指向\ storage \ emulator \ 0中的旧未剪切图像...并且裁剪直接进入我的图库。如何裁剪úí和裁剪后的图像在我的画廊中 - 如何将裁剪后的图像添加到我的图像中

代码:

        private static final int PICK_IMAGE = 0;
        private static final int PICK_IMAGE_FROM_GALLERY = 1;
        private static final int CROP_IMAGE = 2;
        private Uri uri;
.
.
.            


         btnPhotoCamera.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    Intent camera=new Intent();
                    camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                    //camera.putExtra("crop", "true");

                    File f=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

                    uri = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myFile.jpg"));
                    camera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    startActivityForResult(camera, PICK_IMAGE);
                }
            });
        private void performCrop(Uri picUri)
        {
            //NEXUS 5 OS 5 is example of this branch
            // take care of exceptions
            try {
                // call the standard crop action intent (the user device may not
                // support it)

                Intent cropIntent = new Intent("com.android.camera.action.CROP");
                // indicate image type and Uri
                cropIntent.setDataAndType(uri, "image/*");
                // set crop properties
                cropIntent.putExtra("crop", "true");
                // // indicate aspect of desired crop
                cropIntent.putExtra("aspectX", 1);
                cropIntent.putExtra("aspectY", 1);
                // // // indicate output X and Y

                // retrieve data on return
                cropIntent.putExtra("return-data", true);
                // start the activity - we handle returning in onActivityResult
                startActivityForResult(cropIntent, CROP_IMAGE);
            }
            // respond to users whose devices do not support the crop action
            catch (ActivityNotFoundException anfe)
            {//NOT TESTED AS HW DOES NOT GO HERE
                Toast toast = Toast.makeText(this,"This device doesn't support the crop action! Exception: " + anfe.toString(),Toast.LENGTH_SHORT);
                toast.show();
            }
        }

      protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            // TODO Auto-generated method stub
            if (resultCode==RESULT_OK )
            {
                if(requestCode == PICK_IMAGE) //reply from camera
                {
                    performCrop(uri); //crop the picture
                }

                if(requestCode == CROP_IMAGE) //reply from crop
                {
                    Bitmap bmp = getBitmap(uri);
                    imgView.setImageBitmap(bmp);
                }
         }
    }


    private Bitmap getBitmap(Uri bitmap_uri) {
        InputStream is=null;
        try
        {
            is = this.getContentResolver().openInputStream(bitmap_uri);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        return BitmapFactory.decodeStream(is);
    }
}

1 个答案:

答案 0 :(得分:0)

裁剪结果将在onActivityResult中的额外日期中显示如下:

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    if(requestCode == CROP_IMAGE && resultCode == RESULT_OK) //reply from crop
     {
          Bundle extras = data.getExtras();  
          if(extras != null ) {  
             Bitmap photo = extras.getParcelable("data");  
             FileOutputStream fOut = new FileOutputStream(tmpFile);
             photo.compress(Bitmap.CompressFormat.JPEG, 75, fOut);  
                //etc 
         }
     }
}