【问题标题】:Unable to download Images from Firebase Storage无法从 Firebase 存储下载图片
【发布时间】:2019-07-04 08:37:00
【问题描述】:

我正在尝试从名称设置为用户的用户 ID 的 firebase 下载个人资料图片。我正在使用 glide 库下载图像,但我收到 StorageException: StorageException has occurred. Object does not exist at location. 错误。

这是我的代码

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) {}

    });

我的数据库

存储参考声明

StorageReference storageReference;

storageReference = FirebaseStorage.getInstance().getReference();

【问题讨论】:

  • 您是否收到此日志Log.d("TAG" , "URI = "+uri);
  • 没有应用崩溃
  • 试试.child(uid + ".jpg")
  • 尝试使用child() 一次,像这样:.child("ProfilePictures/" + uid + ".jpg")
  • 对不起,我是私人的,但我能问一下你为 Firebase 存储设置了什么规则吗?

标签: java android firebase firebase-storage


【解决方案1】:

改变这个:

storageReference.child("ProfilePictures").child(uid).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override

进入这个:

storageReference.child("ProfilePictures").child(uid + ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override

【讨论】:

  • 你确定uid 存在于数据库中吗?您是否尝试在 child(.......) 中对其进行硬编码?
  • D/TAG: UID = 5tI5e4Y2NeOQ7yq8DwefvmYJk713 uid in logcat
  • 不,我的意思是在child()storageReference.child("ProfilePictures").child('5tI5e4Y2NeOQ7yq8DwefvmYJk713.jpg')
  • 也试过了。没有变化
  • @DarrylFernandes 嗨在这里的评论D/TAG: UID = 5tI5e4Y2NeOQ7yq8DwefvmYJk713 uid in logcat你是怎么进入onSuccess里面的???
【解决方案2】:

确保您是否允许用户使用此规则访问存储:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

把它放在你的依赖项中:

dependencies {
    // FirebaseUI Storage only
    implementation 'com.firebaseui:firebase-ui-storage:4.3.1'
}

现在从存储中获取图像:

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);

如需更多信息,您可以阅读docs 或在出现任何问题时评论我

【讨论】:

  • 这有什么问题可以告诉我,我会帮你解决的
  • E/StorageException: StorageException has occurred. Object does not exist at location. 我遇到了同样的错误
  • @DarrylFernandes 我的方法有同样的问题吗?如果这是它的问题,那么我认为您重新检查存储,因为我认为文件名已更改或其他原因
  • 我刚刚又检查了一遍。它在那里
  • 问题是如果你想在你的应用程序中加载图像,需要将其作为 Uri 检索,然后使用 getdownloadurl() 函数提取 Uri。
【解决方案3】:
 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();
            }
        });

现在这应该可以解决您的问题了!如果有帮助,请务必点赞,如有疑问请发表评论!
注意:使用您在代码中提到的 onSuccess() 代替我的 UploadTask 代码函数。

【讨论】:

    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 2016-12-28
    • 2020-02-08
    • 2017-05-16
    相关资源
    最近更新 更多