从 Firebase 存储下载照片
Downloading Photo from Firebase Storage
我已经尝试了我找到的所有方法,但仍然没有用,所以我写在这里
我正在尝试从我的文件夹“images”中的 Firebase 存储下载照片,其中的文件与当前授权用户 ID 同名。这里唯一的问题是它不起作用。仅显示对象的背景(我正在更改 ShapableImageView 上的照片)
下面我从应用程序添加代码、日志和照片。
代码:
private void downloadProfile() {
User localUser = new User("","","0","","",0,0,0);
if(mAuth.getCurrentUser()!=null){
database_ref.child("users").child(mAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
localUser.name = snapshot.child("name").getValue().toString();
localUser.mail = mAuth.getCurrentUser().getEmail();
localUser.age = snapshot.child("age").getValue().toString();
localUser.phone = snapshot.child("phone").getValue().toString();
localUser.address = snapshot.child("address").getValue().toString();
localUser.login_method = Integer.parseInt(snapshot.child("login_method").getValue().toString());
localUser.playlist_amount = Integer.parseInt(snapshot.child("playlist_amount").getValue().toString());
localUser.fav_song_amount = Integer.parseInt(snapshot.child("fav_song_amount").getValue().toString());
profile_email.setText(localUser.mail);
profile_name.setText( localUser.name);
age_edit_t.setText(localUser.age);
phone_edit_t.setText(localUser.phone);
address_edit_t.setText(localUser.address);
// Image download
storageReference = storage.getReferenceFromUrl("gs://altas-notas.appspot.com");
storageReference.child("images/"+mAuth.getCurrentUser().getUid()+".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
profile_img.setImageURI(uri);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
日志:https://pastebin.com/F0hCxZsv
无论我如何尝试下载并将照片设置为透明或默认。
如果重要,我还添加了更改图像的部分代码:
private void startGallery() {
Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
cameraIntent.setType("image/*");
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1000);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super method removed
if (resultCode == RESULT_OK) {
if (requestCode == 1000) {
returnUri = data.getData();
Bitmap bitmapImage = null;
try {
bitmapImage = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), returnUri);
profile_img.setImageBitmap(bitmapImage);
} catch (IOException e) {
e.printStackTrace();
}
//Upload image
storageReference = FirebaseStorage.getInstance().getReference();
storageReference.child("images/"+mAuth.getCurrentUser().getUid()).putFile(returnUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
System.out.println("Upload image is successful!");
}else{
System.out.println("Upload image failed!");
}
}
});
//Upload rest of information
updateProfile();
}
}
}
在您的代码中:
storageReference.child("images/"+mAuth.getCurrentUser().getUid()+".jpg")
您不需要 .jpg
。像这样尝试:
storageReference.child("images/"+mAuth.getCurrentUser().getUid())
我已经尝试了我找到的所有方法,但仍然没有用,所以我写在这里
我正在尝试从我的文件夹“images”中的 Firebase 存储下载照片,其中的文件与当前授权用户 ID 同名。这里唯一的问题是它不起作用。仅显示对象的背景(我正在更改 ShapableImageView 上的照片)
下面我从应用程序添加代码、日志和照片。
代码:
private void downloadProfile() {
User localUser = new User("","","0","","",0,0,0);
if(mAuth.getCurrentUser()!=null){
database_ref.child("users").child(mAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
localUser.name = snapshot.child("name").getValue().toString();
localUser.mail = mAuth.getCurrentUser().getEmail();
localUser.age = snapshot.child("age").getValue().toString();
localUser.phone = snapshot.child("phone").getValue().toString();
localUser.address = snapshot.child("address").getValue().toString();
localUser.login_method = Integer.parseInt(snapshot.child("login_method").getValue().toString());
localUser.playlist_amount = Integer.parseInt(snapshot.child("playlist_amount").getValue().toString());
localUser.fav_song_amount = Integer.parseInt(snapshot.child("fav_song_amount").getValue().toString());
profile_email.setText(localUser.mail);
profile_name.setText( localUser.name);
age_edit_t.setText(localUser.age);
phone_edit_t.setText(localUser.phone);
address_edit_t.setText(localUser.address);
// Image download
storageReference = storage.getReferenceFromUrl("gs://altas-notas.appspot.com");
storageReference.child("images/"+mAuth.getCurrentUser().getUid()+".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
profile_img.setImageURI(uri);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
日志:https://pastebin.com/F0hCxZsv
无论我如何尝试下载并将照片设置为透明或默认。
如果重要,我还添加了更改图像的部分代码:
private void startGallery() {
Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
cameraIntent.setType("image/*");
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1000);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super method removed
if (resultCode == RESULT_OK) {
if (requestCode == 1000) {
returnUri = data.getData();
Bitmap bitmapImage = null;
try {
bitmapImage = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), returnUri);
profile_img.setImageBitmap(bitmapImage);
} catch (IOException e) {
e.printStackTrace();
}
//Upload image
storageReference = FirebaseStorage.getInstance().getReference();
storageReference.child("images/"+mAuth.getCurrentUser().getUid()).putFile(returnUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()){
System.out.println("Upload image is successful!");
}else{
System.out.println("Upload image failed!");
}
}
});
//Upload rest of information
updateProfile();
}
}
}
在您的代码中:
storageReference.child("images/"+mAuth.getCurrentUser().getUid()+".jpg")
您不需要 .jpg
。像这样尝试:
storageReference.child("images/"+mAuth.getCurrentUser().getUid())