在这种情况下,当发送到Firebase时如何压缩图像?]

问题描述 投票:0回答:2
我正在为Android设备(例如WhatsApp)开发应用程序。它已经处于非常高级的状态,我正在纠正遇到的小问题。但是我很难解决这个问题。

目标是压缩配置文件和封面照片,以占用Firebase Storage更少的空间。然后,压缩将必须同时适用于个人资料和封面图像。

private void uploadProfileCoverPhoto(final Uri uri) { //show progress pd.show(); /*Instead of creating separate function for profile picture and cover photo this will work in the same function*/ //path and name of image to be stored in firebase storage String filePathAndName = storagePath+ ""+ profileOrCoverPhoto +"_"+ user.getUid(); StorageReference storageReference2nd = storageReference.child(filePathAndName); storageReference2nd.putFile(uri) .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { //image uploaded to storage, now get its url and store in users database Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl(); while (!uriTask.isSuccessful()); final Uri downloadUri = uriTask.getResult(); //check if image is uploading or not and url received if (uriTask.isSuccessful()){ //image uploaded //add/update url in users database HashMap<String, Object> results = new HashMap<>(); /*first parameter is profileorcover photo thas has value "image" or "cover" which are keys in users database where url of the image * be saved in of them * Second parameter contains the url of the image stored in firebase storage, this url will be saved as value against key "image" or "cover"*/ results.put(profileOrCoverPhoto, downloadUri.toString()); databaseReference.child(user.getUid()).updateChildren(results) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { //URL IN DATA BASE of user is add succesfully //dismiss progress bar pd.dismiss(); Toast.makeText(getActivity(), "Image Updated.", Toast.LENGTH_SHORT).show(); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { //error adding url in database of user //dismiss progress bar pd.dismiss(); Toast.makeText(getActivity(), "An error occurred updating the image.", Toast.LENGTH_SHORT).show(); } }); //if user edit his name, also change it from hist posts if (profileOrCoverPhoto.equals("image")){ DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts"); Query query = ref.orderByChild("uid").equalTo(uid); query.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for (DataSnapshot ds: dataSnapshot.getChildren()){ String child = ds.getKey(); dataSnapshot.getRef().child(child).child("uDp").setValue(downloadUri.toString()); } } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); //update user image in current users comments on posts ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for (DataSnapshot ds: dataSnapshot.getChildren()){ String child = ds.getKey(); if (dataSnapshot.child(child).hasChild("Comments")){ String child1 = ""+dataSnapshot.child(child).getKey(); Query child2 = FirebaseDatabase.getInstance().getReference("Posts").child(child1).child("Comments").orderByChild(uid).equalTo(uid); child2.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for (DataSnapshot ds: dataSnapshot.getChildren()){ String child = ds.getKey(); dataSnapshot.getRef().child(child).child("uDp").setValue(downloadUri.toString()); } } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); } } } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); } } else{ //error pd.dismiss(); Toast.makeText(getActivity(), "An error has occurred.", Toast.LENGTH_SHORT).show(); } } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { //there were some error, get and show error message, dismiss progress dialog pd.dismiss(); Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show(); } }); }

我正在为Android设备(例如WhatsApp)开发应用程序。它已经处于非常高级的状态,我正在纠正遇到的小问题。但这我很难解决...
java android firebase firebase-storage compress-images
2个回答
0
投票
非常抱歉,您共享的代码无法为您提供帮助。但我可以建议您使用一个可以为您压缩图像的库。单击上传后,您可以运行压缩库。

0
投票
我正在使用此对象将图像压缩到最大1Mo:
© www.soinside.com 2019 - 2024. All rights reserved.