简体   繁体   English

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

[英]How to resize images uploaded to Cloud Storage to a specified size?

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.我在我的应用程序中使用RecyclerView让教授查看学生列表,其中有照片和其他详细信息以及删除选项。 Everything I tried to do carefully.我努力做的每一件事都小心翼翼。 But there is a problem that photo from firebase is not loading into the RecyclerView .但是存在一个问题,来自 firebase 的照片没有加载到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.当我的应用程序正在加载时,我正在观察 android Studio 上的运行选项。 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 :这是我对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".我想将所有图像加载到RecyclerView中而不会出现错误行“不活动,与服务断开连接”。 How can I solve this?我该如何解决这个问题?

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.在将照片上传到Firebase时,您可以压缩照片大小然后上传。 So that when your android app gets photo from Firebase then the image size will be less and it can allocate easily.这样当您的android appFirebase获取照片时,图像尺寸会更小,并且可以轻松分配。

  2. You can use refer to https://developer.android.com/topic/performance/graphics/load-bitmap to load heavy or large images into android efficiently.您可以参考https://developer.android.com/topic/performance/graphics/load-bitmap有效地将重或大图像加载到 android 中。

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.由于您的Firebase存储将被使用更少,用户可以在App中轻松下载您的图像,而不会出现任何Lagmemory问题。

The best solution I can think of would be to use the Resize Images Extension :我能想到的最佳解决方案是使用Resize Images Extension

Resizes images uploaded to Cloud Storage to a specified size, and stores both the original and resized images.将上传到 Cloud Storage 的图片调整为指定大小,并同时存储原始图片和调整大小的图片。

Here is the Github repo .这是Github存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调整上传的图像大小以便更好地查看 - Resize uploaded images for better viewing 如何在云存储上检查 Firebase 的上传文件的文件类型? - How to check the filetype of an uploaded file on Cloud Storage for Firebase? Google云端存储:上传的图片已损坏 - Google Cloud Storage: Uploaded image is corrupt Java Google Cloud Storage API:如何将新上传的对象设置为公共 - Java Google Cloud Storage API: How to set new uploaded objects public 在启动画面上将png调整大小或缩放到指定大小 - resize or scale png to specified size on splashscreen 如何调整目录中图像的大小? - How to resize images in a directory? 上载到Google云端存储时,输出数据会以随机顺序显示 - Output data appears in a random order when uploaded to google cloud storage 在 Google Cloud Storage 中上传的 PDF 总是显示空白页 - Uploaded PDF in Google Cloud Storage always show blank page 无法将图像上传到 Firebase 云存储 - Not able to upload images to Firebase Cloud Storage 如何获取本地开发服务器中Google云存储上载文件的公共链接(Google App Engine + JAVA) - How to get public link for the uploaded file on google cloud storage in local dev server(Google App engine+JAVA)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM