裁剪后设置 ImageView 不起作用

Setting ImageView after Crop not working

我创建了一个带有 imageView 和两个按钮的基本 activity。 1 个按钮打开图库,另一个按钮打开相机。两者都将结果传递到图像裁剪库中。裁剪后的图像被保存并显示在 imageView 中。

第一次我使用任一按钮执行此操作时,一切正常。然而,在第二次尝试时,imageView 没有被替换,但已保存的图像已更改。

所以基本上,如果 imageView 已经有了第一个结果,它就不会更改为新图像。

代码如下:

public void takeDisplayPicture(View view) {

    final String TAKE_DISPLAY_PICTURE = "Take Display Picture";

    Log.d(TAKE_DISPLAY_PICTURE, "Clicked");

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

        File photoFile = null;

        try {

            photoFile = createImageFile();

        } catch (IOException e) {

            Log.e(TAKE_DISPLAY_PICTURE, "Error Occurred");

        }

        if (photoFile != null) {

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));
            Log.d(TAKE_DISPLAY_PICTURE, photoFile.getAbsolutePath());
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

        }

    } else {

        Log.d(TAKE_DISPLAY_PICTURE, "No Camera");

        Toast.makeText(getApplicationContext(),"No Camera Available",Toast.LENGTH_SHORT).show();

    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent result) {

    final String ON_ACTIVITY_RESULT = "On Activity Result";

    Log.d(ON_ACTIVITY_RESULT, "Triggered");

    Log.d(ON_ACTIVITY_RESULT, "Request Code: "+Integer.toString(requestCode)+" Result Code: "+Integer.toString(resultCode));

    Uri img = Uri.parse("file:///"+Config.APP_PATH+Config.APP_USER_PATH+"/"+Config.DISPLAY_PICTURE_NAME+".png");

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

        Log.d(ON_ACTIVITY_RESULT, "Beginning Crop");

        Log.d(ON_ACTIVITY_RESULT,img.toString());

        beginCrop(img, img);

    } else if (requestCode == Crop.REQUEST_CROP) {

        Log.d(ON_ACTIVITY_RESULT, "Handling Crop");

        handleCrop(resultCode, result);

    } else if (requestCode == 2){

        Log.d(ON_ACTIVITY_RESULT, "Gallery Image Returned");

        File file = new File(img.getPath());
        file.delete();

        Uri src = result.getData();

        Log.d(ON_ACTIVITY_RESULT, src.toString());

        beginCrop(src, img);

    }

}

private void beginCrop(Uri source, Uri output){

    final String BEGIN_CROP = "Begin Crop";

    Log.d(BEGIN_CROP, "Beginning");

    new Crop(source).output(output).asSquare().start(this);

}

private void handleCrop(int resultCode, Intent result) {

    final String HANDLE_CROP = "Handle Crop";

    if (resultCode == RESULT_OK) {

        Log.d(HANDLE_CROP, "Set ImageView");
        displayPicture.setImageURI(Crop.getOutput(result));

        try {

            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Crop.getOutput(result));

        } catch (Exception e ){

            Log.d(HANDLE_CROP, "Error Occurred");

        }

    } else if (resultCode == Crop.RESULT_ERROR) {

        Log.d(HANDLE_CROP, "Error Occurred");

    }
}

String currentPhotoPath;

private File createImageFile() throws IOException {

    final String CREATE_IMAGE_FILE = "Create Image File";

    String fileName = "display_picture";

    File dir = new File(Config.APP_PATH+Config.APP_USER_PATH);

    dir.mkdirs();

    File image = new File(dir,fileName+".png");

    return image;
}

public void chooseFromGallery(View view) {

    final String CHOOSE_FROM_GALLERY = "Choose From Gallery";

    Log.d(CHOOSE_FROM_GALLERY, "Clicked");

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}

如果你阅读setImageURI

  if (mResource != 0 ||
            (mUri != uri &&
             (uri == null || mUri == null || !uri.equals(mUri)))) {
                  //Set URI...
  }

基本上,setImageURI 只有在图像的 Uri 不等于您当前要设置的 Uri 时才有效。您的按钮第二次不起作用,因为每次按下按钮时 Crop.getOutput(result) returns 相同的 Uri,因此 setImageURI 什么都不做。

为了解决这个问题,你可以在displayPicture.setImageURI(Crop.getOutput(result));之前添加displayPicture.setImageURI(null);