android - setImageBitmap 没有可见效果

标签 android imageview

我有一个字节数组,其中包含从网上获取的图像。我使用 Bitmapfactory、BitmapDrawable 和 setImageDrawable 将它们懒惰地加载到我的 UI Activity 中(或者我正在尝试至少 :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,但根本没有效果。没有错误,什么都没有。 请问哪里错了?谢谢

最佳答案

这是一个内存不足的问题,您可以使用下面的方法修复它, “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;
}

关于android - setImageBitmap 没有可见效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8039329/

相关文章:

Android SDK初学者问题

android - 我的交换两个 View 背景的代码不起作用

android - 使用 Novoda 的 ImageLoader 显示图像下载进度

java - 如何打败这个 java.net.ProtocolException : Connection already established in Android

安卓 : Refresh GridView in Fragment

java - Android LoaderManager 已弃用。怎么办?

android - 如何从我的服务类中获取显示对象?

Android:在某些设备上调用 notifyDataSetChanged 时, ListView 中的图像会闪烁

android - 单击android ListActivity中的项目不起作用

Android:带有*正确旋转*图像的快速图像 slider