运行时在android中裁剪图像

时间:2011-07-29 06:42:42

标签: android android-2.2-froyo gestures uiviewanimation

我已经使用Intent在我的活动的ImageView中选择了SD卡中的图像。现在我想显示一个固定大小的移动矩形,即我们必须使用手势和我们想要的图像的任何部分,然后我们能够我们能做到这一点吗?对我来说真的很难吗? 请帮帮我这样做?

更新 - >我已经能够使用矩形,我在裁剪和保存选定的部分时遇到问题。如何做到这一点?

2 个答案:

答案 0 :(得分:7)

好的geetanjali。尝试这个代码,这将打开画廊,你可以选择一张照片进行裁剪,它将存储名称从苹果开始,你可以在你的活动中看到裁剪的图像

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


        }
        }
    }

答案 1 :(得分:1)

此博客文章解释了select and crop image in android,希望它有所帮助。