在RecyclerView中加载图像时出现OutOfMemoryError

时间:2016-10-31 21:05:38

标签: android out-of-memory android-image

帮我解决问题。

我在下面提供了我的代码和错误日志 调用OutOfMemory时出现setImageResource错误。

主要Activity.java

package com.example.hetal13.recycler_card_demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    int[] img_id={R.drawable.hetal,R.drawable.surbhi,R.drawable.bhoomi,R.drawable.meghna,R.drawable.aditi};

    String[] name,email,mobile;
    ArrayList<Contact> arrayList = new ArrayList<Contact>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=(RecyclerView) findViewById(R.id.recycle);
        name=getResources().getStringArray(R.array.person_name);
        email=getResources().getStringArray(R.array.person_email);
        mobile=getResources().getStringArray(R.array.person_contact);
        int i =0;
        for(String pname : name){
            Contact contact = new Contact(img_id[i],pname,email[i],mobile[i]);
            arrayList.add(contact);
            i++;
        }
        //recyclerView=(RecyclerView) findViewById(R.id.recycle);
        adapter = new ContactAdapter(arrayList);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }
}

ContactAdapter.java

package com.example.hetal13.recycler_card_demo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Hetal13 on 01-11-2016.
 */

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactView> {
    ArrayList<Contact> contacts =new ArrayList<Contact>();

    public  ContactAdapter(ArrayList<Contact> contacts){
        this.contacts=contacts;
    }
    @Override
    public ContactView onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
        ContactView contactView = new ContactView(view);

        return contactView;
    }

    @Override
    public void onBindViewHolder(ContactView holder, int position) {
Contact contact =contacts.get(position);
        holder.img_id.setImageResource(contact.getImg_id());
        holder.name.setText(contact.getName());
        holder.email.setText(contact.getEmail());
        holder.mobile.setText(contact.getMobile());
    }

    @Override
    public int getItemCount() {
        return contacts.size();
    }
public static class ContactView extends RecyclerView.ViewHolder{
    ImageView img_id;
    TextView name,email,mobile;

    public ContactView(View view)
    {
        super(view);
         img_id=(ImageView) view.findViewById(R.id.img_person);
        name=(TextView)view.findViewById(R.id.person_name);
        email=(TextView)view.findViewById(R.id.person_email);
        mobile=(TextView) view.findViewById(R.id.person_contact);
    }
}
}

Contact.java

package com.example.hetal13.recycler_card_demo;


public class Contact  {
private int img_id;
    private String email,mobile,name;
    public Contact(int img_id,String name,String email,String mobile)
    {
        this.setImg_id(img_id);
        this.setName(name);
        this.setEmail(email);
        this.setMobile(mobile);
    }
    public int getImg_id() {
        return img_id;
    }

    public void setImg_id(int img_id) {
        this.img_id = img_id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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.example.hetal13.recycler_card_demo.MainActivity">
   <android.support.v7.widget.RecyclerView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/recycle"
       android:scrollbars="vertical">
   </android.support.v7.widget.RecyclerView>
</RelativeLayout>

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/card_view">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:padding="10dp">
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/img_person"
            android:src="@drawable/hetal"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/img_person"
            android:text="Hetal Shah"
            android:textSize="20dp"
            android:textStyle="bold"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="20dp"
            android:layout_alignParentTop="true"
            android:id="@+id/person_name"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/person_name"
            android:layout_toRightOf="@+id/img_person"
            android:text="hetalshah027@gmail.com"
            android:textSize="18dp"
            android:textStyle="bold"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="20dp"
            android:id="@+id/person_email"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/person_email"
            android:layout_toRightOf="@+id/img_person"
            android:text="Mobile:7041764049"
            android:textSize="18dp"
            android:textStyle="bold"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="20dp"
            android:id="@+id/person_contact"
            />

    </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

依赖

    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.android.support:cardview-v7:+'

错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.hetal13.recycler_card_demo, PID: 3144
                  java.lang.OutOfMemoryError: Failed to allocate a 548695740 byte allocation with 1430730 free bytes and 253MB until OOM
                      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2474)
                      at android.content.res.Resources.loadDrawable(Resources.java:2381)
                      at android.content.res.Resources.getDrawable(Resources.java:787)
                      at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:133)
                      at android.content.Context.getDrawable(Context.java:403)
                      at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
                      at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:352)
                      at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
                      at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
                      at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:72)
                      at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
                      at com.example.hetal13.recycler_card_demo.ContactAdapter.onBindViewHolder(ContactAdapter.java:33)
                      at com.example.hetal13.recycler_card_demo.ContactAdapter.onBindViewHolder(ContactAdapter.java:16)
                      at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
                      at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
                      at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2788)
                      at android.view.View.measure(View.java:17547)
                      at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
                      at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
I/Process: Sending signal. PID: 3144 SIG: 9
Application terminated.

帮我解决我的错误

0 个答案:

没有答案