【问题标题】:ERROR_UNKNOWN downloading file from Firebase StorageERROR_UNKNOWN 从 Firebase 存储下载文件
【发布时间】:2021-04-16 16:54:01
【问题描述】:

我正在尝试从存储中下载文件 *.Apk 并在完成后安装。 我从存储和使用方面修改了规则。

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read : if request.auth.uid != null;
    }
  }
}

在规则监视器上,所有操作都被接受,但文件无法下载

下载代码片段是:

public void downloadUpdate() {
    StorageReference gsReference = FirebaseStorage.getInstance("filename.apk");
    final String rutadestino = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
    final String nombrearchivo = "LeaderBoard-Upd.apk";
    final Uri archivodestino = Uri.parse("file://" + rutadestino+nombrearchivo);
    File localFile = new File(rutadestino+nombrearchivo);
    if (localFile.exists()) {
        localFile.delete();
    }

    gsReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
             //Local temp file has been created
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", new File(rutadestino+nombrearchivo));
                Intent install = new Intent(Intent.ACTION_VIEW);
                install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                install.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                install.setData(contentUri);
                startActivity(install);
                //unregisterReceiver(this);
                finish();
            } else {
                Intent install = new Intent(Intent.ACTION_VIEW);
                install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                install.setDataAndType(archivodestino,
                        "application/vnd.android.package-archive");
                startActivity(install);
                //unregisterReceiver(this);
                finish();
            }
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
            int errorCode = ((StorageException) exception).getErrorCode();
            String errorMessage = exception.getMessage();
            Emergente.ErrorCode(1,getApplicationContext());
        }
    });
}

我收到了 错误代码“13000” errorMessage "发生未知错误,请检查服务器响应的 HTTP 结果代码和内部异常。" 导致“打开失败:EACCES(权限被拒绝)”

也许另一个代码片段是错误的,但此刻我卡在 OnFailureListener 上

【问题讨论】:

  • 您正在寻找一个格式如下的 URL:http://storage.googleapis.com/&lt;bucket&gt;/&lt;object&gt;,您可以在 GCS docs 上查看更多信息。发布完整的 URL 时也要小心,因为它可能很危险。
  • 对不起,我混淆了存储提供商。但问题是一样的

标签: firebase android-studio firebase-storage


【解决方案1】:

问题出在应用权限上,当应用在没有所需权限的情况下尝试下载文件并将其写入错误的目录/文件时,它会抛出“ERROR_UNKNOWN”消息。

解决方案是在同一个应用程序目录下创建文件,这样做时不需要权限,除非 API

解决方案:

File localFile = new File(getExternalFilesDir(null), "file.apk");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2019-12-10
    • 2017-08-22
    相关资源
    最近更新 更多