如何将上传到Cloud Storage的图像调整为指定大小?

问题描述 投票:0回答:2

我正在应用程序中使用RecyclerView给教授,以查看学生列表,其中将有照片和其他详细信息以及删除选项。我尽力做的所有事情。但是存在一个问题,即来自Firebase的照片无法加载到RecyclerView中。有时会加载一两张照片,然后停止加载照片。我正在加载应用程序时观察android Studio上的“运行”选项。然后我发现类似...

    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

为什么这个问题发生在我身上,我真的还没明白。请帮我摆脱这个问题。

[寻找了很多东西之后,我尝试了一些步骤,据此使缓存无效/重启。但这对我不起作用。

这是我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();
        }
    });
}
}

这是我的适配器活动

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();
            }
        });
    }
}
}

我想将所有图像加载到RecyclerView中,而不会出现错误行“不活动,正在与服务断开连接”。我该如何解决?

java android firebase firebase-realtime-database firebase-storage
2个回答
0
投票

我能想到的最佳解决方案是使用Resize Images Extension

将上传到Cloud Storage的图像调整为指定大小,并同时存储原始图像和调整后的图像。

这里是Github repo


1
投票

您显示的错误表示图像尺寸很大。

您可以做的是

  1. 将照片上传到Firebase时,您可以压缩照片尺寸然后上传。这样,当您的android appFirebase获取照片时,图像尺寸会减小,并且可以轻松分配。

  2. [您可以使用参考https://developer.android.com/topic/performance/graphics/load-bitmap将沉重或庞大的图像有效地加载到android中。

首先将被视为所有人的方式。由于Firebase存储器的使用量将减少,并且用户可以在App中轻松下载图像,而不会出现Lagmemory问题。

© www.soinside.com 2019 - 2024. All rights reserved.