ImageView.setImageBitmap不在RecyclerView中显示图像

时间:2016-12-22 17:09:33

标签: android bitmap imageview

我在RecyclerView中显示图像,其源是从MMS消息中获取的位图。问题是图像没有显示。绝对没有显示任何内容。这是我的onBindView:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    final String name = mDataset.get(position).getContact() ;
    final MMSMessage message = mDataset.get(position);
    holder.txtHeader.setText(name);
    DateTime dateTime = new DateTime(message.getDate());
    holder.txtDate.setText(dateTime.toString(Globals.generalSQLFormatterDT));
    holder.txtText.setText(message.getBody());
    holder.txtText.setVisibility(View.VISIBLE);
    Bitmap bitmap = message.getBitmap();
    if (bitmap != null) {
        //bitmap is not null and I can see an image using Android Studio
        bitmap =Bitmap.createScaledBitmap(bitmap, 120, 120, false); 
        holder.imgMMS.setImageBitmap(bitmap);

    } else {
        holder.imgMMS.setVisibility(View.GONE);
    }

}

ImageView的xml:

    <ImageView
    android:layout_below="@+id/thirdLine"
    android:id="@+id/imageMMS"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="6dip"
    android:contentDescription="TODO"
     />

我看了here并试图将图像缩小到任意小的尺寸。我不认为这是内存不足的错误 - 我尝试将启动器图标作为测试。我做错了什么?

1 个答案:

答案 0 :(得分:1)

if (bitmap != null) {
        //bitmap is not null and I can see an image using Android Studio
        bitmap =Bitmap.createScaledBitmap(bitmap, 120, 120, false); 
        holder.imgMMS.setImageBitmap(bitmap);
holder.imgMMS.setVisibility(View.GONE);
    } else {
        holder.imgMMS.setVisibility(View.GONE);
    }

您正在设置GONE的可见性。我的猜测是RecyclerView正在回收视图,当它看到它时,视图是GONE,因为你没有将它设置为Visible。尝试添加holder.imgMMS.setVisibility(View.VISIBLE);当bitmap不为null时,如下所示:

if (bitmap != null) {
        //bitmap is not null and I can see an image using Android Studio
        bitmap =Bitmap.createScaledBitmap(bitmap, 120, 120, false); 
        holder.imgMMS.setImageBitmap(bitmap);
        holder.imgMMS.setVisibility(View.VISIBLE);
    } else {
        holder.imgMMS.setVisibility(View.GONE);
    }