我有一个应用程序,用户拍照并将图片加载到ImageView中,然后保存在FirebaseStorage中。当互联网速度很慢时,我在使用Firebase(使用Picasso)获取图像时遇到问题。如何在转换为字节数组并保存到Firebase之前减小位图的大小?
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == getActivity().RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageDrawable(makeImageRounded(imageBitmap));
detailViewModel.uploadPhotoToFirebase(imageBitmap);
}
}
答案 0 :(得分:0)
在App中设置权限
select (case when genres like 'Fiction%' then 'Fiction'
when genres like 'Nonfiction%' then 'Nonfiction'
when genres like 'Historical%' then 'Historical'
else 'Other'
end) as genre, count(*)
from books
group by (case when genres like 'Fiction%' then 'Fiction'
when genres like 'Nonfiction%' then 'Nonfiction'
when genres like 'Historical%' then 'Historical'
else 'Other'
end);
声明imageView和uri
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Firebase存储声明
private Uri filePath;
private final int PICK_IMAGE_REQUEST = 71;
在onCreate中启动
storage = FirebaseStorage.getInstance(); storageReference = storage.getReference();
按下按钮
FirebaseStorage storage;
StorageReference storageReference;
onActivityResult您将获得文件路径
private void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
将图片上传到firebase
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null )
{
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
减少特定位图的大小以保存在firebase上
private void uploadImage() {
if(filePath != null)
{
StorageReference ref = storageReference.child("images/"+ UUID.randomUUID().toString());
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(this, "Uploaded", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
}
});
}
}
调用方法 - public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}