ImageView的不清爽/反射变化 [英] ImageView not refreshing/reflecting changes

查看:97
本文介绍了ImageView的不清爽/反射变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是照片选择器意图选择图像,并将其写入到一个应用程序的私人文件。我的大多数重要来源$ C ​​$ C如下所示。有一次,我preSS一个按钮,并执行第一次图像选择的意图,那么它成功地更新图像视图。但是,一旦我preSS图像选择按键(在相同的活动),然后将图像视图不更新,除非我退出并重新启动该活动。所以我知道该图像是越来越成功保存,但为什么会在布局中的ImageView不刷新或更新?

I'm using a photo picker intent to choose an image and write it to an application-private file. Most of my important source code is shown below. Once I press a button and perform the first image selection intent, then it successfully updates the image view. However, once I press the image selection button again (in the same activity), then the image view does NOT update, unless I exit and restart the activity. So I know the image is getting successfully saved, but why would the ImageView in the layout not refresh or update?

public void onResume() {
    super.onResume();
    ImageView myImageView = (ImageView)findViewById(R.id.contact_image);
    if (hasImage) {
      myImageView.setImageURI(Uri.fromFile(getFileStreamPath(TEMP_PHOTO_FILE)));
    }
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case PHOTO_PICKED:
      if (resultCode == RESULT_OK) {
        if (data != null) {
          Bundle extras = data.getExtras();
          if (extras != null) {
            hasImage = true;
            bmp = (Bitmap) extras.get("data");
          }
        }
      }
      break;
    }
  }

  private OnClickListener mChooseImage = new OnClickListener() {
    @Override
    public void onClick(View v) {
      try {
        // Launch picker to choose photo for selected contact
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType("image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", ICON_SIZE);  
        intent.putExtra("outputY", ICON_SIZE);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile()));
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        startActivityForResult(intent, PHOTO_PICKED);
      } catch (ActivityNotFoundException e) {
        // LOG THIS
      }
    }
  };

private File getTempFile() {
    try {
      if (!hasImage) {
        FileOutputStream fos = openFileOutput(TEMP_PHOTO_FILE, MODE_WORLD_WRITEABLE);
        fos.close();
      }
      return getFileStreamPath(TEMP_PHOTO_FILE);
    } catch (FileNotFoundException e) {
      // To be logged later
      return null;
    } catch (IOException e) {
      // To be logged later
      return null;
    }
  }

在活动的结果,我设置的ImageView图像URI到该文件。

upon activity result, I set the ImageView's image URI to this file.

当它第一次完成,ImageView的变化来反映这一点。不过,如果我试图重新选择图像(相同的活动),ImageView的将不会更新,直到我退出并重新进入活动。我不知道为什么会这样,是不是因为我想写信给temp.jpg每次?或者,我需要以某种方式刷新我的布局,以反映变化的ImageView?

When it first completes, the ImageView changes to reflect this. However, if I attempt to choose the image again (same activity), the ImageView will not update until I exit and re-enter the activity. I'm not sure why this happens, is it because I'm trying to write to the temp.jpg everytime? Or do I need to refresh my layout somehow to reflect changes in the ImageView?

推荐答案

在ImageView的源$ C ​​$ C来看,ImageView的将不会重新加载图像,如果你调用setImageURI具有相同URI。您可以尝试通过编写图像到另一个文件改变了URI。

Judging by the ImageView source code, the ImageView won't reload the image if you call setImageURI with the same URI. You could try changing the URI by writing your image to another file.

这篇关于ImageView的不清爽/反射变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆