Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setImageBitmap has no visible effect

I have a byte array which contains an image fetched from the net. I am lazily loading them on my UI activity (or i am trying to at least :D ) using Bitmapfactory, BitmapDrawable and setImageDrawable. here is my code:

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

here is the XML involved

<?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>

I have added "android:src..." just to check if the imageview is visible, and it is. The only problem is that i cannot change it! I have tried setImageBitmap, using only the bitmap i created, i tried setimageDrawable creating a BitmapDrawable, but no effects at all. no errors, no nothing. Please, where is the mistake? thank you

like image 220
STT LCU Avatar asked Nov 07 '11 16:11

STT LCU


Video Answer


1 Answers

It's an out of memory problem, you can fix it using this method as below, The "inSampleSize" option will return a smaller image and save memory. You can just call this in the 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;
}
like image 113
Luna Kong Avatar answered Oct 19 '22 16:10

Luna Kong