0

I am using RecyclerView in my application for the professor to see the students list where there will be photos and other details and delete options. Everything I tried to do carefully. But there is a problem that photo from firebase is not loading into the RecyclerView. Sometimes it loads one or two photos then it stops loading photo. I was observing the Run option on android Studio while my app is loading. Then I found something like...

    W/art: Throwing OutOfMemoryError "Failed to allocate a 71663628 byte     allocation with 4188416 free bytes and 46MB until OOM"
    V/FA: Inactivity, disconnecting from the service

Why this problem happening to me, I really didn't get yet. Please, help me to get life from this problem.

After looking for a lot, I tried some steps where said to invalidate caches/ restart. But that didn't work for me.

Here is my main activity for RecyclerView:

public class HRequestedStudents extends AppCompatActivity {
DatabaseReference databaseReference;
ProgressDialog progressDialog;
List<RegisteredStudent> list = new ArrayList<>();
RecyclerView recyclerView;
RecyclerView.Adapter adapter ;
RequestedStudents madapter;
RegisteredStudent registeredStudent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hrequested_students);
    recyclerView = (RecyclerView) findViewById(R.id.recycleView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(HRequestedStudents.this));

    progressDialog = new ProgressDialog(HRequestedStudents.this);
    progressDialog.setMessage("Loading Data...");
    progressDialog.show();
    databaseReference = FirebaseDatabase.getInstance().getReference("RegisteredStudent");
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            list.clear();
            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                registeredStudent = dataSnapshot.getValue(RegisteredStudent.class);
                list.add(registeredStudent);
            }
            adapter = new RequestedStudents(HRequestedStudents.this, list);
            recyclerView.setAdapter(adapter);
            progressDialog.dismiss();
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });
}
}

Here is my adapter activity

public class RequestedStudents extends RecyclerView.Adapter<RequestedStudents.RecyclerViewHolder> {
private Context mContext;
ProgressDialog progressDialog;
List<RegisteredStudent> requestedStudents;
FirebaseStorage mFirebaseStorage = getInstance();
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference("RegisteredStudent");

public RequestedStudents(Context context, List<RegisteredStudent> list) {
    mContext=context;
    requestedStudents = list;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.hrstudentlistitems, parent, false);
    return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
    RegisteredStudent currentStudentDetails = requestedStudents.get(position);
    holder.name.setText("Name: "+currentStudentDetails.getRegeName());
    holder.room.setText("Room: "+currentStudentDetails.getRegeRoom());
    holder.id.setText("ID: "+currentStudentDetails.getRegeID());
    holder.batch.setText("Batch: "+currentStudentDetails.getRegeBatch());
    holder.dept.setText("Department: "+currentStudentDetails.getRegeDept());
    holder.email = currentStudentDetails.getRegeEmail();
    holder.nam = currentStudentDetails.getRegeName();
    holder.rum = currentStudentDetails.getRegeRoom();
    holder.mobile = currentStudentDetails.getRegeMobile();
    holder.pass = currentStudentDetails.getRegePass();
    holder.iD = currentStudentDetails.getRegeID();
    holder.Batch = currentStudentDetails.getRegeID();
    holder.Dept = currentStudentDetails.getRegeDept();
    holder.imageUri = currentStudentDetails.getImageUri();
    Picasso.get().load(currentStudentDetails.getImageUri()).into(holder.profile_photo);
}

@Override
public int getItemCount() {
    return requestedStudents.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
    public TextView name, id, room, batch, dept;
    public String email, mobile, imageUri, pass, iD, nam, rum, uId, Batch, Dept;
    public CircleImageView profile_photo;
    public Button remove, accept;

    public RecyclerViewHolder(View item) {
        super(item);
        name = item.findViewById(R.id.name);
        room = item.findViewById(R.id.room);
        id = item.findViewById(R.id.id);
        batch = item.findViewById(R.id.batch);
        dept = item.findViewById(R.id.dept);
        profile_photo = item.findViewById(R.id.profile_photo);
        remove = item.findViewById(R.id.remove);
        accept = item.findViewById(R.id.accept);

        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog = new ProgressDialog(mContext);
                progressDialog.setMessage("Please Wait...");
                progressDialog.show();
                LayoutInflater inflater = LayoutInflater.from(mContext);
                View view = inflater.inflate(R.layout.student_bistarito, null);

                CircleImageView imagebi = view.findViewById(R.id.imagebi);
                TextView nambi = view.findViewById(R.id.nambi);
                TextView rumbi = view.findViewById(R.id.rumbi);
                Button call = view.findViewById(R.id.call);
                Button cancel = view.findViewById(R.id.cancel);

                final AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
                nambi.setText("Name: " + nam);
                rumbi.setText("Room: " + rum);
                Picasso.get().load(imageUri).into(imagebi);
                progressDialog.dismiss();

                uId = FirebaseAuth.getInstance().getUid();


                call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(Intent.ACTION_CALL);
                        i.setData(Uri.parse("tel:" + mobile));
                        if (ContextCompat.checkSelfPermission(mContext, CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                            mContext.startActivity(i);
                        } else {
                            Toast.makeText(mContext, "Go to settings and allow call permission", Toast.LENGTH_LONG).show();
                        }
                        alertDialog.dismiss();
                    }
                });
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            Toast.makeText(mContext, "Shubho", Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                        alertDialog.dismiss();
                    }
                });
                alertDialog.setView(view);
                alertDialog.show();
                alertDialog.getWindow().setBackgroundDrawable(null);
            }
        });
        profile_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                View vi = inflater.inflate(R.layout.activity_photo, null);
                ImageView photo = vi.findViewById(R.id.image);
                Button close = vi.findViewById(R.id.close);
                final AlertDialog ad = new AlertDialog.Builder(mContext).create();
                Picasso.get().load(imageUri).into(photo);
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ad.dismiss();
                    }
                });
                ad.setView(vi);
                ad.show();
                ad.getWindow().setBackgroundDrawable(null);
            }
        });
        accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("Delete");
                builder.setMessage("Are you sure to remove this student?");
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Query query = mRef.orderByChild("email").equalTo(email);
                            query.addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                                        ds.getRef().removeValue();
                                    }
                                    Toast.makeText(mContext, "Removed Successfully!", Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError databaseError) {
                                    Toast.makeText(mContext, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            });
                            StorageReference mPictureReferance = getInstance().getReferenceFromUrl(imageUri);
                            mPictureReferance.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    Toast.makeText(mContext, "Removed Successfully!", Toast.LENGTH_SHORT).show();
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            });

                        } catch (Exception e) {
                            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
        });
    }
}
}

I want to load all images into the RecyclerView without getting the error line "Inactivity, Disconnecting from the service". How can I solve this?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

2 Answers2

1

The error you showed upside means that the image size is very large.

What you can do is

  1. While uploading photos to Firebase you can compress the photo size and then upload it. So that when your android app gets photo from Firebase then the image size will be less and it can allocate easily.

  2. You can use refer to https://developer.android.com/topic/performance/graphics/load-bitmap to load heavy or large images into android efficiently.

First will be considered as the way everyone do. As your Firebase storage will be used less and users can download your images easily in App without any Lag or memory problem.

Pratham Khurana
  • 141
  • 1
  • 12
  • There is now a built-in Firebase extension for resizing images: https://firebase.google.com/products/extensions/storage-resize-images/ – charles-allen Nov 10 '19 at 08:07
0

The best solution I can think of would be to use the Resize Images Extension:

Resizes images uploaded to Cloud Storage to a specified size, and stores both the original and resized images.

Here is the Github repo.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Check the "Detailed configuration information" section as well as the official documentation regarding [extensions](https://firebase.google.com/docs/extensions/) and tell me if it works. – Alex Mamo Nov 11 '19 at 10:05
  • Thanks, Alex. I installed the Resize Images Extension on my firebase console. Now, do I need any change in the code of my activity file to upload the image getting the resized image and how can I get the resized image's download uri? – Shubho Sikder Nov 11 '19 at 10:42
  • Sorry for duplicate comments. But really how can I get the resized image download uri? – Shubho Sikder Nov 11 '19 at 10:45
  • [This](https://www.youtube.com/watch?v=9o8oenmUBYg) is how you install and this is how you [get the image URI](https://stackoverflow.com/questions/53299915/how-to-get-offline-uploaded-file-download-url-in-firebase/53300660#53300660). Tell me it works. – Alex Mamo Nov 11 '19 at 10:48
  • Alex, I am uploading the photo of which url is IMG_3555.jpg, but the resized image's url is IMG_3555_500x500.jpg. Following your link, I already used that to get the original image's url. So, how can I get the updated image url? – Shubho Sikder Nov 11 '19 at 11:50
  • To get the resized images you need to access the `sezises` folder. In that folder you'll find all sezises images. Having the name of image, you can simply recreate the new name by adding `_500x500` to the original name or using [list(int)](https://firebase.google.com/docs/reference/android/com/google/firebase/storage/StorageReference.html#list(int)) method. Is it ok now? – Alex Mamo Nov 11 '19 at 11:58