【问题标题】:cannot change imageview src after closing camera intent关闭相机意图后无法更改 imageview src
【发布时间】:2012-10-05 19:35:05
【问题描述】:

我有一个小问题。

代码目标:单击打开相机的imageview,然后在获取图片后更改imageview。 imageview 不是照片的缩略图。

问题:imageview 不刷新。

我尝试过的:Android imageView Switching Problem, Changing ImageView on button click, ImageView onClickListener changing the image source (不能使用 viewSwitcher b/c 有两个以上可能的视图组合), how to change the background or the image source when a clickable imageview in android is clicked?

我的代码:

public class CacTestActivity extends Activity {

private static final int CAMERA_PIC_REQUEST = 2500;
private Bitmap bmp;
private ImageView ivPic;
private Thread thread;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cactest);

    ivPic = (ImageView) findViewById(R.id.imgCacSetPhoto);

    ivPic.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, CAMERA_PIC_REQUEST);
            } catch (Exception e) {
            }
        }
    });
    thread = new Thread(){
        @Override
        public void run(){
            ivPic = (ImageView) findViewById(R.id.imgCacSetPhoto);
            ivPic.setImageResource(R.drawable.takephotoyes);
            ivPic.invalidate();
        }
    };
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    if (resultCode == RESULT_OK){
        if (requestCode == CAMERA_PIC_REQUEST){
            //If getting pic bitmap is successful, then change imageview source
            //thread.start();       //Does not work
        }
    }
}
}

这个问题之前已经被问过很多次了,但是经过多次搜索我还没有找到解决方案。我错过了什么?谢谢

【问题讨论】:

  • 你为什么要在另一个线程上调用setImageResource?更改 UI 的操作(例如此操作)应在 UI 线程上运行,因此无需在另一个线程中运行。
  • @James:使用线程是对 SO 的建议。在浪费了几个小时之后,我找到了解决方案——它在模拟器上根本不起作用。更改 onActivityResult 中的 imageview src 适用于我的实际手机。 [如果有人想知道,我使用了 ivPic.setImageResource() 而不是我代码中的线程] 这让我有点担心。那我该如何彻底测试呢?模拟器针对 API 级别 10。这是一个已知问题吗?
  • 清单中的目标 API 是什么? Eclipse 项目属性中的目标 android sdk 是什么?
  • 你是怎么做出这样的推断的?杰出的!那就是问题所在。当我更改项目构建目标以匹配(较低)清单目标 api 级别时,模拟器中的图像 src 发生了变化。谢谢@詹姆斯。我阅读了this,但是对于项目构建目标与 targetSdkVersion,还有其他好的来源吗?
  • 我想感谢 James 的解决方案或将我的问题标记为已回答,但我不知道这是如何完成的。

标签: android android-imageview


【解决方案1】:

确保 Eclipse 项目属性中的目标 Android 版本与清单中的 targetSdk 匹配。

  • 右键单击项目 > 属性 > Android 选项卡,并确保所选的 Android 目标与您的 targetSdk 匹配

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多