0

how to resolve "java.lang.OutOfMemoryError: Failed to allocate a 996567422 byte allocation with 7088560 free bytes and 117MB until OOM" in android.

Because of large images size i am getting this problem,i added these two lines in manifest file

android:hardwareAccelerated="false" , android:largeHeap="true"

then also not working,how to resolve the OutOfMemoryError

this is my logcat

04-05 12:01:43.287 11314-17193/info.sherpify.pavithracs.sherpifys E/art: Throwing OutOfMemoryError "Failed to allocate a 996567422 byte allocation with 7078106 free bytes and 117MB until OOM"
04-05 12:01:43.289 11314-17193/info.sherpify.pavithracs.sherpifys E/AndroidRuntime: FATAL EXCEPTION: Thread-88543
                                                                                    Process: info.sherpify.pavithracs.sherpifys, PID: 11314
                                                                                    java.lang.OutOfMemoryError: Failed to allocate a 996567422 byte allocation with 7078106 free bytes and 117MB until OOM
                                                                                        at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:316)
                                                                                        at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:526)
                                                                                        at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:549)
                                                                                        at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:392)
                                                                                        at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:155)
                                                                                        at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:85)

see the below coding i am setting the image to imageview by using picasso.

package info.sherpify.pavithracs.sherpifys.adapter;

/**
 * Created by pavithracs on 11/1/17.
 */

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.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import info.sherpify.pavithracs.sherpifys.R;
import info.sherpify.pavithracs.sherpifys.model.Categorieslist;
import info.sherpify.pavithracs.sherpifys.utilis.NetworkUtils;


public class RecyclerViewAdapterSearch extends RecyclerView.Adapter<RecyclerViewAdapterSearch.ViewHolder> {

    private List<Categorieslist> items;
    private OnItemClickListener onItemClickListener;

    public RecyclerViewAdapterSearch(List<Categorieslist> items) {
        this.items = items;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_home_search, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Categorieslist item = items.get(position);
        Log.i("Items mentor",""+item);

        try {


            holder.textmentordesignation.setText(item.getFieldUserDesig());
            holder.textyearofexperience.setText(item.getFieldUserExp()+" Years");
            holder.textmentorname.setText(item.getFieldUserName());
            holder.textcurrentcompany.setText("Current: "+item.getFieldUserCurrentCompany());
            holder.textpreviouscompany.setText("Previous: "+item.getFieldUserPreviousCompany());

            Log.i("Field--",""+item.getFieldUserDesig());
            Log.i("Field--",""+item.getFieldUserName());
            Log.i("Field--",""+item.getFieldUserExp());



            holder.image.setImageBitmap(null);
            Picasso.with(holder.image.getContext()).load(NetworkUtils.BASE_IMAGE_URL_USER_PICS +item.getFieldUserProfilepic()).resize(50, 50).error(R.drawable.com_facebook_profile_picture_blank_portrait).into(holder.image);

           holder.itemView.setTag(item);
            holder.relativementor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onItemClickListener.onItemClick(v,items.get(position).getFieldUserId(),items.get(position).getFieldUserName(),items.get(position).getFieldUserDesig(),items.get(position).getFieldUserSummary(),items.get(position).getFieldUserProfilepic(),items.get(position).getFieldUserCurrentCompany(),items.get(position).getFieldMentorId());
                }
            });


        } catch (NullPointerException e) {
            e.printStackTrace();
        }


    }

    @Override
    public int getItemCount() {
        return items.size();
    }


    protected static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textmentordesignation,textyearofexperience,textmentorname,textcurrentcompany,textpreviouscompany;
        public ImageView image;
        RelativeLayout relativementor;

        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            textmentordesignation = (TextView) itemView.findViewById(R.id.textmentordesignation);
            textyearofexperience = (TextView) itemView.findViewById(R.id.textyearsofexperience);
            textmentorname = (TextView) itemView.findViewById(R.id.textmentorname);
            textcurrentcompany = (TextView) itemView.findViewById(R.id.textcurrentcompany);
            textpreviouscompany = (TextView) itemView.findViewById(R.id.textpreviouscompany);
            relativementor = (RelativeLayout) itemView.findViewById(R.id.relativementor);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, String viewModel, String cName, String uId, String catId,String catProfilepic,String currentcompany,String mentorid);
    }

}
pavithra
  • 105
  • 1
  • 8
  • 1
    Please add full Error Log output! – Ben Krig Apr 05 '17 at 05:38
  • I doubt you will be able to load that much data in a lot of Android device ... how many images are you loading to get 996567422byte ( = 996.567422Mb) – AxelH Apr 05 '17 at 05:38
  • Possible duplicate of [Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM](http://stackoverflow.com/questions/32244851/androidjava-lang-outofmemoryerror-failed-to-allocate-a-23970828-byte-allocatio) – Dileep Patel Apr 05 '17 at 05:53
  • i edited my question ,will you please check and give me the sloutions – pavithra Apr 05 '17 at 06:36

2 Answers2

0

can you check with glide:

build.gradle:

dependencies { compile 'com.github.bumptech.glide:glide:3.7.0' }

Log.d("imageurl", ""+NetworkUtils.BASE_IMAGE_URL_USER_PICS+item.getFieldUserProfilepic());           

Glide.with(context).load(NetworkUtils.BASE_IMAGE_URL_USER_PICS +item.getFieldUserProfilepic()).override(600, 200).error(R.drawable.com_facebook_profile_picture_blank_portrait).into(holder.image);

and also please post the debugged image url, if possible

Stephen
  • 9,899
  • 16
  • 90
  • 137
0

Try this, create folder name drawable-nodpi put all your placeholder or on error image com_facebook_profile_picture_blank_portrait inside that folder, and replace your code for loading image with picasso with the following

Picasso
    .with(holder.image.getContext())
    .load(NetworkUtils.BASE_IMAGE_URL_USER_PICS +item.getFieldUserProfilepic())
    .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) //add this line
    .resize(50,50)
    .error(R.drawable.com_facebook_profile_picture_blank_portrait)
    .into(holder.image);
Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64