You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio Java项目上传图片至Firebase Storage遇404错误求助

Firebase Storage上传图片报错:Object does not exist at location(Code: -13010)解决思路

你遇到的这个404错误通常不是控制台权限配置问题,而是本地文件访问或Storage路径的问题,结合你的代码和排查情况,给你几个针对性的解决方向:

1. 修复图片URI的访问权限问题

用ActivityResultContracts.PickVisualMedia获取的content://类型URI,Firebase的putFile方法可能无法直接获取足够权限访问该文件。可以改用文件流上传来规避这个问题,修改uploadPicture方法如下:

private void uploadPicture(){
    StorageReference pictureReference = storageReference.child(category.getId()+".jpg");

    try {
        InputStream inputStream = getContentResolver().openInputStream(imageUri);
        pictureReference.putStream(inputStream)
                .addOnFailureListener(exception -> {
                    Toast.makeText(ListingActivity.this, "Could not store image", Toast.LENGTH_LONG).show();
                    System.out.println(exception.getMessage());
                })
                .addOnSuccessListener(taskSnapshot -> {
                    Toast.makeText(ListingActivity.this, "Successfully stored image", Toast.LENGTH_SHORT).show();
                    if(inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(ListingActivity.this, "Failed to access selected image", Toast.LENGTH_SHORT).show();
    }
}

或者也可以给URI添加临时持久权限,在上传前加入:

getContentResolver().takePersistableUriPermission(imageUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

2. 验证Storage路径的有效性

虽然你确认了storage引用正常,但还是要排查最终的文件路径是否合法:

  • 在uploadPicture方法中添加日志,打印完整路径:
Log.d("StorageDebug", "Full storage path: " + pictureReference.getFullPath());
  • 检查category.getId()是否为空或无效值,如果ID为空,会生成类似/.jpg的非法路径,导致上传失败。
  • 确认Listing.LISTING_PATH的格式是否正确,不要以斜杠开头或结尾(Firebase Storage路径不需要首尾斜杠)。

3. 补充适配旧版本的存储权限

如果你的应用需要兼容Android 10(API 29)以下的设备,需要添加读取外部存储权限:
在AndroidManifest.xml中加入:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="29"/>

并在运行时请求该权限(仅针对旧版本设备)。

4. 检查Firebase Storage规则的细节

确保你的规则允许写入,比如测试阶段可以用宽松规则验证(上线后再收紧):

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

内容的提问来源于stack exchange,提问作者Wuhib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:43:15