我要通過recyclerview在CardView中顯示圖像。有15張圖片共15毫克大小。當我在genymotion模擬器(定製手機4.1.1 Api16規範)上運行應用程序時,出現內存不足錯誤。 錯誤是在這條線:在加載15圖像時出現18662412字節的分配錯誤內存不足
foodViewHolder.txtImage.setImageResource(food.getConverted_image());
這是我的適配器
package com.example.android.dezcook;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import org.w3c.dom.Text;
import java.io.IOException;
import java.util.List;
/**
* Created by Android on 6/11/2016.
*/
public class Food_Adapter extends RecyclerView.Adapter<Food_Adapter.foodViewHolder>{
private List<Food> foodList;
private Context context;
public Food_Adapter(List<Food> foodList,Context context)
{
this.foodList=foodList;
this.context=context;
}
@Override
public foodViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView= LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.card_layout,viewGroup,false);
return new foodViewHolder(itemView);
}
@Override
public int getItemCount() {
return foodList.size();
}
@Override
public void onBindViewHolder(foodViewHolder foodViewHolder, int i) {
Food food=foodList.get(i);
foodViewHolder.txtName.setText(food.getTxtName());
foodViewHolder.txtItems.setText(food.getItems());
foodViewHolder.txtId.setText(Integer.toString(food.getTxtid()));
foodViewHolder.txtImage.setImageResource(food.getConverted_image());
}
//food.getTxtImage()
public static class foodViewHolder extends RecyclerView.ViewHolder {
protected TextView txtName;
protected TextView txtItems;
protected ImageView txtImage;
protected TextView txtId;
public foodViewHolder(View v) {
super(v);
txtName=(TextView)v.findViewById(R.id.txtName);
txtItems=(TextView)v.findViewById(R.id.txtItems);
txtImage=(ImageView)v.findViewById(R.id.card_thumbnail);
txtId=(TextView)v.findViewById(R.id.txtid);
}
}
}
我出示該卡的方式:
public void show_card()
{
RecyclerView reclist=(RecyclerView)findViewById(R.id.cardList);
reclist.setHasFixedSize(true);
LinearLayoutManager llm=new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
reclist.setLayoutManager(llm);
Food_Adapter fa=new Food_Adapter(Configure_bank(),this);
reclist.setAdapter(fa);
}
和我的名片佈局XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:onClick="show_details"
android:id="@+id/cardview"
android:background="@color/colorlighter"
card_view:cardCornerRadius="5dp"
tools:showIn="@layout/activity_second">
<RelativeLayout
android:background="@color/colorlighter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp"
android:layout_margin="0dp"
>
<RelativeLayout
android:background="@drawable/xml_rounded_corner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
>
<TextView
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="10dp"
android:id="@+id/txtName"
android:background="@drawable/xml_header_background"
android:textStyle="bold"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemcontainer"
android:layout_toRightOf="@+id/card_thumbnail"
android:layout_below="@+id/header"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="مواد مورد نیاز:"
android:textStyle="bold"
android:layout_alignParentRight="true"
android:textColor="@color/cardview_dark_background"
android:id="@+id/itemid"
/>
<TextView
android:layout_below="@+id/itemid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtItems"
android:layout_alignParentRight="true"
android:textSize="16dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:lineSpacingExtra="5dp"
android:textColor="@color/cardview_dark_background"
/>
</RelativeLayout>
<ImageView
android:id="@+id/card_thumbnail"
android:layout_below="@+id/header"
android:layout_width="120dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:paddingBottom="5dp"
android:layout_alignParentLeft="true"
android:layout_height="120dp"
android:src="@drawable/z8"
android:scaleType="centerCrop"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="@+id/txtid"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
15MB?它們在內存中可能會更大,尤其是在自動縮放發生的情況下。 –
18662412〜2160x2160圖片....爲什麼你需要這麼大的圖像? ... @SamiKuhmonen寫道...磁盤上的大小並不重要,位圖總是採用'W * H * pixelSize'(其中像素大小通常爲(ARGB_8888)爲4個字節) – Selvin
我需要以最高質量 – Jumong