在我的Android应用的AccountActivity
中,用户可以更改其个人资料设置(例如图片,姓名,电子邮件等)。
我编写的代码将获取用户选择的图像的URI,并将其上传到Firebase Storage,然后获取其下载URL 并将其存储在Cloud Firestore中。
但是在将图像上传到存储设备之前,我想压缩图像Uri,然后将原始图像和压缩后的图像上传到存储设备。最后,我想下载压缩图像的网址,以在活动中显示它。
我尝试了很多代码,而这是我最后尝试的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK) {
accountImageProgressBar.setVisibility(View.VISIBLE);
Uri fileUri = data.getData();
StorageReference accountImagesReferences = storageReference.child("Users Images").child(userID + "/" + fileUri.getLastPathSegment());
Bitmap bitmap = ((BitmapDrawable) accountImage.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bytes = baos.toByteArray();
// Upload To Storage
UploadTask uploadTask = accountImagesReferences.putBytes(bytes);
uploadTask
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
accountImageProgressBar.setVisibility(View.INVISIBLE);
Toast.makeText(AccountActivity.this, "Upload Failed!", Toast.LENGTH_SHORT).show();
}
})
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
accountImageProgressBar.setVisibility(View.INVISIBLE);
Toast.makeText(AccountActivity.this, "Upload Success", Toast.LENGTH_SHORT).show();
}
});
// Get Download Url
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();
}
// Continue with the task to get the download URL
return accountImagesReferences.getDownloadUrl();
}
})
.addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
firebaseFirestore.collection("Users").document(userID).update("image", downloadUri.toString());
loadInfo(userID);
} else {
Toast.makeText(AccountActivity.this, "Task Not Successful", Toast.LENGTH_SHORT).show();
}
}
});
}
}
答案 0 :(得分:0)
这将完全满足您的要求。如果不是,那是因为“用户”未通过firebase进行身份验证,因此您需要通过电子邮件或匿名身份验证进行注册
private static final int RESULT_GALLERY = 1;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_GALLERY);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_GALLERY:
if (null != data) {
imageUri = data.getData();
UploadImages();
}
break;
default:
break;
}
}
private byte[] compress(Uri image){
Uri selectedImage = image;
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(
selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 20, stream);
byte[] byteArray = stream.toByteArray();
try {
stream.close();
stream = null;
return byteArray;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void UploadImages() {
final StorageReference ref = FirebaseStorage.getInstance().getReference().child("/images/");
//Uploading original file.
UploadTask uploadTask = ref.putFile(imageUri);
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();
}
// Continue with the task to get the download URL
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
//Original file download url.
final StorageReference ref2 = FirebaseStorage.getInstance().getReference().child("Compressed");
String downloadUri = String.valueOf(task.getResult());
//Uploading compressed file.
byte[] data = compress(imageUri);
UploadTask uploadTask = ref2.putBytes(data);
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();
}
// Continue with the task to get the download URL
return ref2.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
//Compressed file download url
String downloadUri = String.valueOf(task.getResult());
}
});
}
});
}
}