2016-06-16 20 views
2

我要通過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> 
+1

15MB?它們在內存中可能會更大,尤其是在自動縮放發生的情況下。 –

+0

18662412〜2160x2160圖片....爲什麼你需要這麼大的圖像? ... @SamiKuhmonen寫道...磁盤上的大小並不重要,位圖總是採用'W * H * pixelSize'(其中像素大小通常爲(ARGB_8888)爲4個字節) – Selvin

+0

我需要以最高質量 – Jumong

回答

1

使用畢加索庫,通過在磁盤上的gradle這個依賴加入這個
compile 'com.squareup.picasso:picasso:2.5.2'

Picasso 
.with(getActivity()) 
.load(mostPopular) 
.get("your_image") 
.fit() 
.into("your_imageview"); 
0

你真的沒有需要這樣大小的圖像。只需使用Picasso加載和調整圖像大小,例如:

public void onBindViewHolder(final ImageViewHolder holder, int position) { 
    Picasso.with(context) 
      .load(itemList.get(position).getLink()) 
      .error(R.drawable.error_image) 
      .placeholder(R.drawable.progress_animation) 
      .into(holder.image); 
      .into(new Target() { 
       @Override 
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
        int targetWidth = parentWidth/2; 
        float ratio = (float) bitmap.getHeight()/(float) bitmap.getWidth(); 
        float heightFloat = ((float) targetWidth) * ratio; 

        final android.view.ViewGroup.MarginLayoutParams layoutParams 
          = (ViewGroup.MarginLayoutParams) holder.image.getLayoutParams(); 

        layoutParams.height = (int) heightFloat; 
        layoutParams.width = (int) targetWidth; 
        holder.image.setLayoutParams(layoutParams); 
        holder.image.setImageBitmap(bitmap); 
       } 

       @Override 
       public void onBitmapFailed(Drawable errorDrawable) { 
       } 

       @Override 
       public void onPrepareLoad(Drawable placeHolderDrawable) { 
       } 
      }); 
} 

因此,您可以擺脫OutOfMemory問題。

+0

圖像位於資源文件夾(txtImage.setImageResource)中,而不是來自URL,最好仔細查看問題。 – Neo

+0

@Neo和?畢加索也可以從資源加載圖像調整大小... – Selvin

+0

@亞歷山大我認爲不需要「'.into(新目標(){...})'」,因爲畢加索會自動調整大小以適應ImageView – Selvin

相關問題