setImageBitmap has no visible effect

£可爱£侵袭症+ 提交于 2019-12-08 15:41:56

问题


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


回答1:


My guess is you're not doing this in the right thread. You could test this out by calling this code, then rotating the screen (or opening the physical keyboard, if you have one of those phones). This will cause the UI to refresh.

Anyway, what context are you calling this in? Background thread? You'd want to update the UI in the UI thread, or you'll have to call something like 'invalidate' on the View to get it to force a re-draw.




回答2:


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;
}



回答3:


The below solution worked for me. I used Picasa library.

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/

Call the below function to set the bitmap into ImageView

bitmapIntoImageView(imageView, bitmap, MainActivity.this)


来源:https://stackoverflow.com/questions/8039329/setimagebitmap-has-no-visible-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!