2011-11-07 64 views
10

我有一個字節數組,其中包含從網絡中獲取的圖像。我懶惰地加載他們在我的UI活動(或者我想至少:D)使用Bitmapfactory,BitmapDrawable和setImageDrawable。這裏是我的代碼:setImageBitmap沒有可見的效果

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> 

我已經加入「機器人:SRC ......」只是檢查是否ImageView的是可見的,並且它是。唯一的問題是我無法改變它! 我已經嘗試setImageBitmap,只使用我創建的位圖,我試圖setimageDrawable創建一個BitmapDrawable,但沒有任何影響。沒有錯誤,沒有任何東西。 請問,錯誤在哪裏?謝謝

+1

它是否有助於後iv.setImageDrawable(繪製)來調用iv.invalidate()? –

+0

如果我在代碼中添加invalidate(),那麼也沒什麼變化 –

+2

,這要歸功於那些沒有說出任何建設性內容的人。歡呼聲,男人。 –

回答

5

我的猜測是你沒有在正確的線程中這樣做。你可以通過調用這個代碼來測試,然後旋轉屏幕(或者打開物理鍵盤,如果你有一部手機的話)。這將導致用戶界面刷新。

無論如何,你在什麼情況下調用它?後臺線程?你需要在UI線程中更新UI,否則你必須在View上調用'invalidate'來強制重新繪製。

+0

這是一塊UI「主」線程。準確地說,它是被定義爲活動類內部成員的Handler對象。原始圖像字節通過asynctask獲取,並通過我實現的回調方法傳遞給UI線程(因爲它可以在應用程序的其他地方成功使用) –

+0

無論如何,我試圖旋轉屏幕但圖像不刷新無論是。默認圖像仍然顯示。 –

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

我一直沒有管理這個項目,所以我無法測試你的建議。無論如何,我會努力爭取,而且我希望這能夠幫助未來的某個人。謝謝。 –

+0

@STTLCU感謝您的支持。 @。@ –

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) 
+0

畢加索工作得很好 –