如何将上传到Cloud Storage的图像调整为指定大小? [英] How to resize images uploaded to Cloud Storage to a specified size?

查看:66
本文介绍了如何将上传到Cloud Storage的图像调整为指定大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我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中,而不会出现错误行不活动,正在断开与服务的连接".我该如何解决?

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

推荐答案

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

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

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

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

以下是Github repo .

Here is the Github repo.

这篇关于如何将上传到Cloud Storage的图像调整为指定大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆