2015-04-28 71 views
2

我的相機捕捉到圖像後會對圖像進行裁剪 - 所有工作。 該圖像出現在我的畫廊裏所有的裁剪和可愛的 - 所有的工作。 我使用uri將調用裁剪後的圖像分配給imageview - 不工作裁剪圖像並保存好,但imageview有舊圖像,爲什麼?

繼承人我的代碼,我認爲我的問題是,uri指向\ storage \ emulator \ 0中的舊未顯式圖像...裁剪後直接進入我的畫廊。我怎樣才能得到裁剪UI和裁剪後的圖像是在我的畫廊 - 我怎麼裁剪圖像到我的ImageView

代碼:在onActivityResult如下

 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); 
    } 
} 

回答

0

作物的結果將是在特殊的日子:

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 
     } 
    } 
}