java无法将图像上载到firebase
我正在尝试使用firebase上载图像,但我无法这样做。我没有得到什么是错误的,而上传。代码如下:
private void saveUserInformation() {
mCustomerDatabase.updateChildren(userInfo);
if (resultUri != null) {
final StorageReference filepath = FirebaseStorage.getInstance().getReference().child("profileImages").child(userId);
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplication().getContentResolver(), resultUri);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
assert bitmap != null;
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = filepath.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("SA", "Fail1");
finish();
}
});
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Map newImage = new HashMap();
newImage.put("profileImageUrl", uri.toString());
mCustomerDatabase.updateChildren(newImage);
Log.d("SA", "Success");
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.e("SA", "Fail2");
finish();
}
});
}
});
} else {
finish();
}
}
发生StorageException,并且它也不会上载到firebase控制台中。谢谢你的帮助
# 1 楼答案
定义变量
按下imageView时,从库中选择图像
onActivityResult方法
然后是图像的上传代码(按钮)
**备注您应该在OnCreate方法中强制转换uploudBtn、profileImageView**