android - 裁剪后设置 ImageView 不起作用

标签 android image camera imageview

我创建了一个带有 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) 都会返回相同的 Uri,因此 setImageURI 什么都不做。

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

关于android - 裁剪后设置 ImageView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29061283/

相关文章:

Android TextInputLayout/EditText 不是全尺寸和截断文本

iphone - 我们在 iPhone 中有图像捕获事件的通知吗?

javascript - 手机间隙 : Asynchronous calls in javascript or native code?

android - Android 中的自定义联系人列表

android - 如何在android中的图像上应用不同的颜色滤镜?

objective-c - 使用静态库中的图像

javascript - Three.js 点击事件后沿路径移动相机

C# Net CF - 如何在没有交互的情况下拍照?

xml - 在 Android 上编写 XML

ios - 自适应阈值 OpenCV IOS