setImageBitmap没有可见效果。

10

我有一个字节数组,其中包含从网络获取的图像。我正在使用Bitmapfactory、BitmapDrawable和setImageDrawable在我的UI活动上懒加载它们(或者至少我正在尝试:D)。这是我的代码:

RelativeLayout r =(RelativeLayout) adap.getGroupView(Integer.parseInt(groupPos), false, null, null);
ImageView iv = (ImageView) r.findViewById(R.id.imgGruppo);
Log.w("",""+raw_img.length);
Bitmap bm = BitmapFactory.decodeByteArray(raw_img, 0, raw_img.length);
Drawable drawable = new BitmapDrawable(bm);
Log.i("","pre setimage"); 
iv.setImageDrawable(drawable);
//added for testing only, with no effects.
//((ELA) Activity_Titoli_2.this.getExpandableListAdapter()).notifyDataSetChanged();
//ELA is my expandable list adapter
Log.i("","post setimage"+bm.getRowBytes()); //just to see that i have actual data in raw_img and such

这里涉及到的是XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayoutTitoli2_gruppo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textNomeGruppo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Large Text"
        android:textColor="#FF0000"
        android:padding="14dp"
        android:textAppearance="?android:attr/textAppearanceLarge"  />

    <TextView
        android:id="@+id/textNoteGruppo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textNomeGruppo"
        android:paddingLeft="14dp"
        android:paddingRight="14dp"
        android:paddingBottom="7dp"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <ImageView
        android:id="@+id/imgGruppo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/icon"
        />

</RelativeLayout>

我添加了"android:src..."只是为了检查ImageView是否可见,它确实可见。唯一的问题是我无法更改它! 我尝试使用setImageBitmap,仅使用我创建的位图;我尝试使用setimageDrawable创建一个BitmapDrawable,但没有效果。没有错误,什么都没有。 请问,哪里出错了?谢谢。


1
在 iv.setImageDrawable(drawable) 后面调用 iv.invalidate() 会有帮助吗? - Adil B
如果我在代码中添加invalidate(),什么也不会改变。 - STT LCU
2
还有,感谢那个没有提出任何建设性意见就给我点踩的人。拜拜,伙计。 - STT LCU
我也遇到了同样的问题,你最终找出了是什么原因导致的吗? - S-K'
不好意思,这发生在很多年前的旧版Android上,我已经完全不记得了。即使我还记得,我想它也已经不再适用了。 - STT LCU
显示剩余2条评论
3个回答

12

这是一个内存不足的问题,你可以按照下面的方法解决, 使用"inSampleSize"选项将返回较小的图像并节省内存。 在ImageView.setImageBitmap中调用即可。

    private Bitmap loadImage(String imgPath) {
    BitmapFactory.Options options;
    try {
        options = new BitmapFactory.Options();
        options.inSampleSize = 4;// 1/4 of origin image size from width and height
        Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

我已经有一段时间没有管理这个项目了,所以无法测试您的建议。不管怎样,我会为您的努力点赞,并希望这能在未来帮到某些人。谢谢。 - STT LCU

5

我猜测您没有在正确的线程中执行此操作。您可以通过调用此代码,然后旋转屏幕(或打开物理键盘,如果您的手机有),这将导致UI刷新。

无论如何,您在什么上下文中调用此操作?后台线程?您需要在UI线程中更新UI,否则您需要在View上调用类似“invalidate”的东西来强制重新绘制。


这是UI“主”线程的一部分。确切地说,它是定义为活动类的内部成员的Handler对象。原始图像字节通过asynctask获取,并通过我实现的回调方法传递到UI线程(因为在应用程序的其他位置成功使用,所以它有效)。 - STT LCU
无论如何,我尝试旋转屏幕,但图像也没有刷新。默认图像仍然显示。 - STT LCU

1
以下解决方案对我有效。我使用了 Picasa 库。
private Uri getImageUri(Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(activity.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public void bitmapIntoImageView(ImageView imageView, Bitmap bitmap, Activity activity){
    Uri imageUri = getImageUri(bitmap);
    Picasso.with(activity).load(imageUri).into(imageView);
}

public void imagePathIntoImageView(ImageView imageView, String imagePath, Activity activity) {
    Uri imageUri = Uri.fromFile(new File(imagePath));
    Picasso.with(activity).load(imageUri).into(imageView);
}

Picasa: http://square.github.io/picasso/ 调用以下函数将位图设置到ImageView中。
bitmapIntoImageView(imageView, bitmap, MainActivity.this)

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