1
我有一個應用程序,用戶可以在其中創建事件並從圖庫中上傳圖像並將其上傳到FirebaseStorage。一切工作正常,但圖像真的很大,我想優化加載/下載過程。我已經嘗試過這一點:上傳到FirebaseStorage之前的圖像壓縮
// Get the data from an ImageView as bytes
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = mountainsRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type
Uri downloadUrl = taskSnapshot.getDownloadUrl();
}
});
和設置bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
這工作,但這個基本上使得每一個形象醜陋。 如果可能的話,是否有方法可以減少圖像的大小而不會完全破壞質量?