【问题标题】:How to Upload and Retrieve image in latest firebase storage after update更新后如何在最新的 Firebase 存储中上传和检索图像
【发布时间】:2019-10-27 07:37:25
【问题描述】:

我正在尝试上传和检索图像,并按照 Stack 和教程中提供的所有结果进行操作,但没有任何示例对我有帮助。

如何在最新的 Firebase 存储中点击按钮上传图片

我正在关注本教程 https://www.simplifiedcoding.net/firebase-storage-example/

StorageReference reference= storageReference.child(System.currentTimeMillis()+ "."+getFileExtension(filePath));
            mUploadTask= reference.putFile(filePath);
            Task<Uri> urlTask = mUploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }

                    // Continue with the task to get the download URL
                    return storageReference.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if (task.isSuccessful()) {
                        Uri downloadUri = task.getResult();
                    } else {
                        // Handle failures
                        // ...
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            progressDialog.dismiss();
                            Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });

【问题讨论】:

    标签: android firebase firebase-realtime-database firebase-storage image-uploading


    【解决方案1】:

    您上传图片是正确的,在您返回 storageReference.getDownloadUrl() 后,只需将该值设置到您的 firebase 数据库中。

    StorageReference reference= storageReference.child(System.currentTimeMillis()+ "."+getFileExtension(filePath));
                mUploadTask= reference.putFile(filePath);
                Task<Uri> urlTask = mUploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                    @Override
                    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                        if (!task.isSuccessful()) {
                            throw task.getException();
                        }
    
                        // Continue with the task to get the download URL
                        return storageReference.getDownloadUrl();
                    }
                }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task) {
                        if (task.isSuccessful()) {
                            Uri downloadUri = task.getResult();
                            mDatabaseRef.child("images").child("imageUrl").setValue(downloadUri.toString());
                        } else {
                            // Handle failures
                            // ...
                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception exception) {
                                progressDialog.dismiss();
                                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        });
    

    您需要从 firebase 请求该图像 url 字符串,然后在任何 ImageView 中显示图像,使用 PicassoGlide 加载它。

    【讨论】:

      猜你喜欢
      • 2017-05-31
      • 2018-06-04
      • 2021-07-18
      • 2016-06-17
      • 2020-08-01
      相关资源
      最近更新 更多