Reputation: 4330
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
Upvotes: 10
Views: 14327
Reputation: 5914
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)
Upvotes: 1
Reputation: 3155
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;
}
Upvotes: 12
Reputation: 17302
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.
Upvotes: 5