简体   繁体   English

在ListView中添加图像显示内存不足

[英]adding images in ListView shows out of memory

I'm creating the listView that contains image and text but unfortunately it will work for adding three images more than that it shows out of memory.I saw many questions like the same.but still i dint get any clear idea how to call the Bitmap functions within my code. 我正在创建包含图像和文本的listView,但是不幸的是,它将添加三个图像以使其超出内存显示的范围。我看到了很多类似的问题。但是我仍然不清楚如何调用位图我的代码中的功能。

My code is here, help me to complete this task. 我的代码在这里,请帮助我完成此任务。

public class MainActivity extends AppCompatActivity {

String[] titles;
ListView list1;

int [] img = {R.drawable.meal11,R.drawable.meal12,R.drawable.mrngs1,R.drawable.mrngs2,R.drawable.lunch1,R.drawable.lunch2};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list1 = (ListView) findViewById(R.id.list);
    titles = new String[]{"BANANA & BERRY SMOOTHIE","COTTAGE CHEESE, AVOCADO & TOMATO TOAST","FRITTATA CAKES","CHOC PROTEIN BALLS","CHICKEN & SUNDRIED TOMATO SALAD","TUNA WRAP"};

    myAdapter adapter = new myAdapter(getApplicationContext(),titles,img);

    list1.setAdapter(adapter);




}

class myAdapter extends ArrayAdapter<String> {
    Context context;
    int[] imgs;
    String[] titles;
    myAdapter(Context context,String[] titles,int imgs[]) {
        super(context,R.layout.list_item,R.id.text,titles);
        this.context= context;
        this.imgs = imgs;
        this.titles = titles;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View row = inflater.inflate(R.layout.list_item,parent,false);

        ImageView myImage = (ImageView) row.findViewById(R.id.image);

        TextView myText = (TextView) row.findViewById(R.id.texttitle);

        myImage.setImageResource(imgs[position]);

        myText.setText(titles[position]);

        return row;
    }
}}

xml code for mainActiviy.xml: mainActiviy.xml的xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sa.addimagelistview.MainActivity"
android:padding="0dp">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/list">

</ListView>

xml code for list_item.xml: list_item.xml的xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="0.2">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/image"
        android:src="@drawable/meal11"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="0.8">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/texttitle"
        android:textSize="20sp"
        android:text="@string/app_name"/>

</LinearLayout>
</LinearLayout>

The issue here is that, images you are trying to add in your listview are large and after a certain time they exceed the default memory provided by android. 这里的问题是,您试图添加到列表视图中的图像很大,并且经过一段时间后,它们超过了android提供的默认内存。
To over come this you have to scale the images down to the size you actually want them in 为了克服这个问题,您必须将图像缩小到实际需要的尺寸

You can use Piccaso which does the scaling work on its own if you use fit() 您可以使用Piccaso,如果您使用fit() ,它将自行进行缩放

All you have to do is this. 您所要做的就是这个。

add the Picasso dependency in your gradle compile 'com.squareup.picasso:picasso:2.5.2' and then try this 在gradle compile 'com.squareup.picasso:picasso:2.5.2'添加Picasso依赖项,然后尝试执行此操作

Picasso
  .with(context)
  .load(your_image)
  .fit()
  // call .centerInside() or .centerCrop() to avoid a stretched image
  .into(your_imageview);

You can do this with the help of picasso... 您可以在毕加索的帮助下完成此任务...

  Picasso.with(context)
                    .load(imgs[position])//load images into imageview
                    .placeholder(R.drawable.image_file)//default image
                    .into(myImage);

Don't use setImageResource() directly but, use libraries like Glide and Picasso which loads image very smoothly and do image caching to load it fast. 不要直接使用setImageResource(),而要使用GlidePicasso之类的库,它们可以非常平稳地加载图像并进行图像缓存以快速加载它。

Glide Compile Line - compile 'com.github.bumptech.glide:glide:3.7.0' Glide编译行-编译'com.github.bumptech.glide:glide:3.7.0'

Picasso Compile Line - compile 'com.squareup.picasso:picasso:2.5.2' 毕加索编译行-编译'com.squareup.picasso:picasso:2.5.2'

Glide Example: 滑行示例:

Glide.with(context).load(imgs[position]).into(myImage);

Picasso Exaple: 毕加索实例:

Picasso.with(context).load(imgs[position]).into(myImage);

If you don't want to use libraries try this official Android Link . 如果您不想使用库,请尝试使用此官方的Android Link

put in Mainfest---> 放在Mainfest --->

<application
        android:largeHeap="true"
    >
.....
   </application>

may it will help 希望会有所帮助

Here is my modification on your adapter please try this one hope it would help :D 这是我对您的适配器的修改,请尝试这一操作,希望对您有所帮助:D

class myAdapter extends ArrayAdapter<String> {
    Context context;
    int[] imgs;
    String[] titles;
    myAdapter(Context context,String[] titles,int imgs[]) {
        super(context,R.layout.list_item,R.id.text,titles);
        this.context= context;
        this.imgs = imgs;
        this.titles = titles;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView!=null){
            holder=(ViewHolder) convertView.getTag();
        }else{
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.list_item,parent,false);

            holder = new ViewHolder(convertView);

            convertView.setTag(holder);
        }


        holder.myImage = (ImageView) convertView.findViewById(R.id.image);

        holder.myText = (TextView) convertView.findViewById(R.id.texttitle);

        myImage.setImageResource(imgs[position]);

        myText.setText(titles[position]);

        return convertView;
    }

    // somewhere else in your class definition
    static class ViewHolder {
        ImageView myImage;
        TextView myText;    
    }
}

Here is Reference for ViewHolder 是ViewHolder的参考

I had the same problem. 我有同样的问题。 It worked for me if I just opened the image in Gimp, scaled it down and exported it with loss of quality so that it took up less space. 如果我只是在Gimp中打开图像,按比例缩小图像并以降低质量的方式导出图像,那么它就对我有用,从而减少了空间。

Should be some better solution though since the image I had was only a couple of megabytes in the first place. 不过应该是更好的解决方案,因为我最初拥有的映像只有几兆字节。

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

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