【发布时间】:2021-01-15 21:38:52
【问题描述】:
“我正在尝试将个人资料照片上传到 Firebase。但它没有检索到应用程序。当我上传照片时它正在工作。但没有检索。如何解决这个问题?”
'在这里,当用户选择图像时,我们可以在此处裁剪该图像。以及上传到火力基地的图片。'
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Gallery && resultCode == RESULT_OK && data!= null){
Uri imageUri = data.getData();
// start picker to get image for cropping and then use the image in cropping activity
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.start(this);
}
//crop image
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK){
progressDialog.setTitle("Set profile Image");
progressDialog.setMessage("Your profile image is updating..");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
Uri resultUri = result.getUri();
final StorageReference filePath = profileImageReference.child(currentUserID + ".jpg");//new profile image created
//upload image to firebase storage
filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()){
Toast.makeText(SettingsActivity.this, "Profile Image uploaded...", Toast.LENGTH_SHORT).show();
final String downloadUri = filePath.getDownloadUrl().toString();
rootReference.child("Users").child(currentUserID).child("image")
.setValue(downloadUri)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText(SettingsActivity.this, "Image Saved", Toast.LENGTH_SHORT).show();
}
else {
String message = task.getException().toString();
Toast.makeText(SettingsActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
}
else {
String message = task.getException().toString();
Toast.makeText(SettingsActivity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
}
}
}
'在这部分检索用户信息和图像文件。'
private void RetrieveUserInformation() {
rootReference.child("Users").child(currentUserID)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if ((snapshot.exists()) && (snapshot.hasChild("name") && (snapshot.hasChild("image")))){
String retrieveUserName = snapshot.child("name").getValue().toString();
String retrieveDescription = snapshot.child("description").getValue().toString();
String retrieveProfileImage = (String) snapshot.child("image").getValue();
userName.setText(retrieveUserName);
description.setText(retrieveDescription);
Picasso.get().load(retrieveProfileImage).into(profileImage);
}
else if ((snapshot.exists()) && (snapshot.hasChild("name"))){
String retrieveUserName = snapshot.child("name").getValue().toString();
String retrieveDescription = snapshot.child("description").getValue().toString();
userName.setText(retrieveUserName);
description.setText(retrieveDescription);
}
else{
Toast.makeText(SettingsActivity.this, "Please, Update your information...", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
【问题讨论】:
-
首先,停止忽略错误。使用
Log.d(TAG, databaseError.getMessage());。您是否在 logcat 中打印了一些内容?请将您的数据库结构添加为 JSON 文件或至少是屏幕截图。
标签: java android firebase firebase-storage picasso