[英]Unable to download Images from Firebase Storage
I am trying to download the profile picture from firebase whose name is set as the userID of the user. 我正在尝试从名称设置为用户的用户名的Firebase下载个人资料图片。 I am using the glide library to download the images but I am getting a StorageException: StorageException has occurred. Object does not exist at location.
我正在使用滑行库下载图像,但出现StorageException: StorageException has occurred. Object does not exist at location.
StorageException: StorageException has occurred. Object does not exist at location.
error. 错误。
Here is my code 这是我的代码
String uid = user.getUid();
storageReference.child("ProfilePictures").child(uid).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
Log.d("TAG" , "URI = "+uri);
GlideApp.with(context).load(uri).into(profilepic);
//profilepic.setImageURI(uri);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Toast.makeText(getApplicationContext(), "Error getting Profile Picture", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
Declaration of storageReference 储存声明参考
StorageReference storageReference;
storageReference = FirebaseStorage.getInstance().getReference();
Change this: 更改此:
storageReference.child("ProfilePictures").child(uid).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
into this: 到这个:
storageReference.child("ProfilePictures").child(uid + ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
Make sure if you're allowed to users to access Storage with this Rule : 确保是否允许用户使用此规则访问存储:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}
Put this in you're dependencies : 把它放在您的依赖项中:
dependencies {
// FirebaseUI Storage only
implementation 'com.firebaseui:firebase-ui-storage:4.3.1'
}
Now Get Image from storage : 现在从存储中获取图像:
StorageReference storageReference= FirebaseStorage.getInstance().getReference().child("ProfilePictures/"+uid+".jpg"); // if you know how to use this you can get image directly without doing that big query
//Following line will be useful when you try to get image from storage
GlideApp.with(this /* context */)
.load(storageReference)
.into(imageView);
For further information you can read docs or just comment me if any issue occur 有关更多信息,您可以阅读文档或在发生任何问题时仅对我发表评论
public StorageReference mStorageRef;
StorageReference particular_image;
private FirebaseDatabase firebasedatabase;
private DatabaseReference databasereference;
oncreate()
{
mStorageRef = FirebaseStorage.getInstance().getReference().child("give");
firebasedatabase = FirebaseDatabase.getInstance(); //1st time is imp.
databasereference = firebasedatabase.getReference().child("giver_data");
particular_image.putFile(photoURI).addOnSuccessListener
(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
final Uri download_uri = taskSnapshot.getDownloadUrl();
Glide.with(getApplicationContext())
.load(p_l.getPhotoUrl()).asBitmap().override(view.getMaxWidth(),view.getMaxHeight()).error(R.drawable.ic_selfie_point_icon) //asbitmap after load always.
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Bitmap d = new BitmapDrawable(resource).getBitmap();
int nh = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(d, 512, nh, true);
//holder.food_img.setImageBitmap(scaled);
view.setImageBitmap(scaled);
}
});
//getting uri of the image stored
//Photo_link p_link =newPhoto_link(download_uri.toString());
// databasereference.push().setValue(p_link);
//String uri_string = download_uri.toString();
pb.clearAnimation();
pb.clearFocus();
// animation.end();
Intent i = new Intent(getApplicationContext(),Giver_Edit.class);
i.setData(download_uri);
startActivity(i);
Toast.makeText(getApplicationContext(),"Food Image Uploaded successfully",Toast.LENGTH_SHORT).show();
Log.d("giver_image_success","no eroooooor_on_success");
//Toast.makeText(getApplicationContext(),"Image added",Toast.LENGTH_LONG).show();
}
});
// final Uri selectedImgUri = getIntent().getData();
particular_image.putFile(uri).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("giver_image_failure","eroooooor_on_ffailure");
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
And now this should solve your problem! 现在,这应该可以解决您的问题! Make sure to upvote if helpful and comment if doubt! 如果有帮助,请确保投票,如有疑问,请发表评论!
Note: Instead of my code function of UploadTask use your onSuccess() which you had mentioned in the code. 注意:请使用代码中提到的onSuccess()代替我的UploadTask代码功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.