【问题标题】:Cannot retrieve image from Firebase Storage: Object does not exist at location无法从 Firebase 存储中检索图像:该位置不存在对象
【发布时间】:2020-11-22 17:56:21
【问题描述】:

我的应用出现问题。我将图像上传到 Firebase 存储,但现在我无法检索它,我收到一条错误消息:

E/StorageException: StorageException has occurred.
    Object does not exist at location.
     Code: -13010 HttpResult: 404

我的代码如下:

private void getImage() {
        mStorage = FirebaseStorage.getInstance("gs://bucket");
        mStorageReference = mStorage.getReference().child("/animal_images/animals" + System.currentTimeMillis() + ".png");
        final long ONE_MEGABYTE = 1024 * 1024;
        mStorageReference.getBytes(ONE_MEGABYTE).addOnSuccessListener(bytes -> {
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            DisplayMetrics displayMetrics = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            mImageView.setMinimumHeight(displayMetrics.heightPixels);
            mImageView.setMinimumWidth(displayMetrics.widthPixels);
            mImageView.setImageBitmap(bitmap);
        }).addOnFailureListener(e -> Log.e("TAG", "Cannot load image" + e.getMessage()));
    }

我检查了存储路径,一切正常,我在这里做错了什么。任何建议。

【问题讨论】:

    标签: android firebase firebase-storage


    【解决方案1】:

    我认为问题在于您构建图像路径的方式:

    mStorage.getReference().child("/animal_images/animals" + System.currentTimeMillis() + ".png");
    

    似乎极不可能存在具有客户端请求时的当前时间戳的图像 (System.currentTimeMillis())。

    虽然在写入图像的客户端中使用时间戳是增加其文件名唯一的机会的好方法,但您不能使用相同的方法来查找图像.您需要通过某种方式将图像的时间戳写入它的客户端读取它的客户端进行通信。

    【讨论】:

      【解决方案2】:

      当您创建存储的引用时:

      mStorageReference = mStorage.getReference().child("/animal_images/animals" + System.currentTimeMillis() + ".png");
      

      你写了System.currentTimeMillis(),它的值不是恒定的,会不时改变。这意味着您基本上是在搜索由于时间变化而具有更改值的名称。名称为新时间的图像不存在。这就是你找不到它的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-23
        • 1970-01-01
        • 2020-11-13
        • 2023-03-10
        • 2021-03-26
        • 2017-08-30
        相关资源
        最近更新 更多