裁剪后设置ImageView不起作用

3
我创建了一个基本的活动,其中包含一个imageView和两个按钮。一个按钮打开画廊,另一个按钮打开相机。两个按钮都将结果传递给图像裁剪库。裁剪后的图像被保存并显示在imageView中。
第一次使用任何一个按钮时,一切都很顺利。但是在第二次尝试时,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);
}
1个回答

3
如果你阅读了 setImageURI
  if (mResource != 0 ||
            (mUri != uri &&
             (uri == null || mUri == null || !uri.equals(mUri)))) {
                  //Set URI...
  }

基本上,只有在图像的Uri与您想要当前设置的Uri不相等时,setImageURI才起作用。您的按钮第二次不起作用,因为每次按下按钮时,Crop.getOutput(result)返回相同的Uri,所以setImageURI不起作用。

要解决此问题,可以在displayPicture.setImageURI(Crop.getOutput(result))之前添加displayPicture.setImageURI(null);。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接