How to resize images uploaded to Cloud Storage to a specified size?

淺唱寂寞╮ 提交于 2020-01-11 04:15:32

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/58786548/how-to-resize-images-uploaded-to-cloud-storage-to-a-specified-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!