开发者

Runtime Cropping an Image in android

I have already selected an image from SD card in my activity's ImageView using Intent.and now I want to show a fixed size moving Rectangle i.e. we have to use gesture and whatever p开发者_高级运维ortion of the image we want,then we are able to crop that.How can we do that?Its really tough for me to do? Please help me in doing that?

Update-->I have been able to bring the rectangle and I m getting problem in cropping and saving that selected part.How to do this?


ok geetanjali. try this code this will open gallery and you can pick a photo to crop, it will store with name starts from apple, you can see cropped image in your activity

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        photoPickerIntent.putExtra("crop","true");
        photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
        photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
         startActivityForResult(photoPickerIntent, 1);

    }

       private Uri getTempFile() {
       if (isSDCARDMounted()) {
           String f;
           muri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                    "apple_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
       //File f = new File(Environment.getExternalStorageDirectory(),"titus1.jpg");
       try {
        f=muri.getPath();
       } catch (Exception e) {

       }
       return muri;
       } else {
       return null;
       }
      }
   private boolean isSDCARDMounted(){
       String status = Environment.getExternalStorageState();
       if (status.equals(Environment.MEDIA_MOUNTED))
       return true;
       return false;
       }
   protected void onActivityResult(int requestCode, int resultCode,
           Intent imageReturnedIntent) {
       super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

       switch (requestCode) {
       case 1:
           if (resultCode == RESULT_OK) {  
           String filePath= muri.getPath();
           Log.e("path", "filePath");
           Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();

           Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
           image = (ImageView)findViewById(R.id.image);
           image.setImageBitmap(selectedImage);


        }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜