2

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

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

4

1 回答 1

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


        }
        }
    }
于 2011-07-29T07:22:43.537 回答