在将图像上传到Firebase存储之前如何减小图像的大小? [英] How to reduce the size of image before uploading it to firebase storage?
问题描述
我已经制作了一个用于图像和视频共享的社交应用程序.但是,它花费了太多时间来加载图像.我正在使用glide库.请告诉我如何减少从图库中拾取的图像大小图像质量没有大的变化(就像Instagram一样),然后将其上传到Firebase存储.请帮忙!
I have made an social app for image and video sharing.However,its taking too much time for loading the image.I am using the glide library.Please, tell me how can I reduce the size of image picked up from gallery without considerable change in quality of image (like Instagram do) and then upload it to firebase storage.Please help!
推荐答案
StorageReference childRef2 = [your firebase storage path]
storageRef.child(UserDetails.username+"profilepic.jpg");
Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos);
byte[] data = baos.toByteArray();
//uploading the image
UploadTask uploadTask2 = childRef2.putBytes(data);
uploadTask2.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Profilepic.this, "Upload successful", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Profilepic.this, "Upload Failed -> " + e, Toast.LENGTH_LONG).show();
}
});`
只需继续执行上述步骤,它将减小您的图片大小,并将其上传到Firebase上,将图片大小减小至1到2 mb 就像我的经验一样,4mb文件变成了304kb.
just go ahead and do above steps it will reduce your image size and upload it on firebase it reduce the image size upto 1 to 2 mb like on my experience 4mb file became 304kb .
filepath
是所选图像的File
和File
对象. :)
filepath
is and File
object of your selected image. :)
这篇关于在将图像上传到Firebase存储之前如何减小图像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!