问题描述
我制作了一个用于图像和视频共享的社交应用.但是,加载图像需要太多时间.我正在使用滑行库.请告诉我如何减小从图库中拾取的图像的大小图像质量没有显着变化(如 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 mblike on my experience 4mb file became 304kb .
filepath
是所选图像的 File
对象.:)
filepath
is and File
object of your selected image. :)
这篇关于如何在将图像上传到 firebase 存储之前减小图像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!