简体   繁体   English

具有图像和文本的Listview的内存不足异常

[英]Out of memory Exception on Listview with images and text

My App has run out of memory due to a ListView with an ImageView and 2 TextView 's inside. 我的应用程序内存不足,原因是其中包含一个ImageView和2个TextView的ListView。 The images are big and I shrink them down to 56x56dp in single_row.xml for the ListView, but they are also used in larger sizes on other screens. 图片很大,我将它们缩小到ListView的56x56dp中的single_row.xml ,但在其他屏幕上也使用较大的图片。

The MainActivity calls the single_row into the listview and inflates it. MainActivity将single_row调用到列表视图中并对其进行膨胀。 Then the single row is duplicated with individual content for each row. 然后,将单行与每一行的单独内容重复。 Each row has an image and 2 textviews. 每行都有一个图像和2个textview。 I know there's a way to use bitmap but I'm unsure how to implement it in the code. 我知道有一种使用位图的方法,但是我不确定如何在代码中实现它。

Here is the main activity.java code 这是主要的activity.java代码

@Override public View getView(int i, View convertView, ViewGroup viewGroup) {
    View row=convertView;   
    if(row==null){
        LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.single_row, viewGroup,false); 
    }
    TextView title = (TextView) row.findViewById(R.id.textView);
    TextView description = (TextView)row.findViewById(R.id.textView2);
    ImageView image = (ImageView)row.findViewById(R.id.imageView);

    SingleRow temp=list.get(i);
    title.setText(temp.title);
    description.setText(temp.description);
    image.setImageResource(temp.image);

    return row;
}

here is the singlerow.xml 这是singlerow.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:src="@drawable/placeholder1" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        android:text="Large Text"    
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignParentRight="true"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />
    </RelativeLayout>`

Any solution would be helpful. 任何解决方案都将有所帮助。 I'm unsure how to implement it in this specific structure. 我不确定如何在此特定结构中实现它。

As Karakuri said, restricting the ImageView size to 56dpx56dp doesn't mean that the entire bitmap is not loaded. 正如Karakuri所说,将ImageView大小限制为56dpx56dp并不意味着不会加载整个位图。 First load the bitmap from the file (or resource), and shrink it. 首先从文件(或资源)中加载位图,然后缩小它。 Use 采用

//For resource
image.setImageBitmap(decodeSampledBitmapFromResource("android.resource://com.my.package/drawable/image_name"));
 //For file
image.setImageBitmap(decodeSampledBitmapFromResource(filepath));


public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 2;
    if (height >= reqHeight || width >= reqWidth) {
        inSampleSize *= 2;
    }
    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(String file,
        int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(file, options);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM