GetDownloadUrl返回专用链接,不公开,Firebase

时间:2018-10-18 09:05:42

标签: android firebase firebase-storage

private void uploadImage() {
   final ProgressDialog progressDialog = new ProgressDialog(this);
   progressDialog.setTitle("Загрузка...");
   progressDialog.show();
   final StorageReference ref = storageReference.child(avatarUrl);
   ref.putFile(Uri.parse(Settings.uriImage))
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            progressDialog.dismiss();
                            Toast.makeText(Settings.this, "Фото профиля подключено", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            progressDialog.dismiss();
                            Toast.makeText(Settings.this, "Ошибка " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
                                    .getTotalByteCount());
                            progressDialog.setMessage("Прогресс " + (int) progress + "%");
                        }
                    });
        avatarUrlFinal = avatarUrl;
    }
    private void gettingImage () {
        StorageReference islandRef = storageReference.child(avatarUrlFinal);
        Uri test = islandRef.getDownloadUrl().getResult();

现在islandRef.getDownloadUrl()。getResult()返回图像的私有链接(例如gs://xxx.appspot.com/images/xxx),而不是公开链接(对于例如https://firebasestorage.googleapis.com/v0/b/xxx.appspot.com/o/images%xxx)。

如何获取下载的图像的公共链接

1 个答案:

答案 0 :(得分:1)

正确的密码

StorageReference islandRef = storageReference.child(avatarUrlFinal);
        islandRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                String test = uri.toString();
                Toast.makeText(Settings.this, test, Toast.LENGTH_SHORT).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(Settings.this, "Ошибка", Toast.LENGTH_SHORT).show();
            }
        });