android - 如何在上传到 Firebase 之前压缩图像?
问题描述
我正在创建一个社交网络应用程序,用户可以在其中发布图像,因此当用户上传其图像时,它的尺寸非常大,当我检索该图像时,毕加索花费了太多时间。有什么方法可以在上传之前压缩这些图像而不会造成明显的质量损失,以便可以非常有效和快速地检索它们。PS:我使用 Firebase 作为后端服务器。
这是我上传图片的代码。
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == Gallery_Pick && resultCode == RESULT_OK && data != null){
ImageUri = data.getData();
SelectPostImage.setImageURI(ImageUri);
}
super.onActivityResult(requestCode, resultCode, data);
}
final StorageReference filePath = PostImagesRef.child("Post Images").child(ImageUri.getLastPathSegment() + postRandomName + ".jpg");
final UploadTask uploadTask = filePath.putFile(ImageUri);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String message = e.toString();
Toast.makeText(PostActivity.this, "Some Error Occured"+message, Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(PostActivity.this, "Image Uploaded Successfully", Toast.LENGTH_SHORT).show();
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if(!task.isSuccessful()){
throw task.getException();
}
downloadImageUrl = filePath.getDownloadUrl().toString();
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful()){
downloadImageUrl = task.getResult().toString();
Toast.makeText(PostActivity.this, "Saved To Database Successfully", Toast.LENGTH_SHORT).show();
SavingPostInformationToDatabase();
}
}
});
}
});
请帮我。任何帮助或建议都将不胜感激。提前致谢。!!:)
解决方案
您可以简单地使用 Glide 调整图像大小并使用该图像上传到 firebase
Glide.with(requireActivity())
.asBitmap()
.override(YOUR_IMAGE_SIZE, YOUR_IMAGE_SIZE)
.load(uri)
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap>?
) {
// using bitmapToByte(resource) -> byte
// using filePath.putBytes(data) -> uploadTask
val filePath = PostImagesRef.child("Post Images").child(ImageUri.getLastPathSegment() + postRandomName + ".jpg")
val uploadTask = filePath.putBytes(bitmapToByte(resource))
}
override fun onLoadCleared(placeholder: Drawable?) {
// this is called when imageView is cleared on lifecycle call or for
// some other reason.
// if you are referencing the bitmap somewhere else too other than this imageView
// clear it here as you can no longer have the bitmap
}
})
bitmapToByte
功能
fun bitmapToByte(bitmap: Bitmap): ByteArray {
val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
return stream.toByteArray()
}
希望这可以帮助
推荐阅读
- mongodb - mongoose/mongodb 我可以发布但无法获取数据。只返回状态码 500 没有错误
- python - 字段 'id' 需要一个数字,但得到了 'r' - Django 表单
- javascript - 使用不同的数组导航 JSON 文件
- matlab - 计算matlab中连续数字之间包含零的行
- ruby - 通过嵌套数组循环?
- python - 模块脚本中的这些语句实现了什么?
- javascript - 如何影响存储在 localStorage 上的对象值而不覆盖它们?
- c# - C# 运行字节数组而不转储到临时文件
- pine-script - Pine 脚本错误:添加到图表操作失败,原因:限制为 50000
- sql - 通过引用多个表创建断言