更新后如何在最新的Firebase存储中上载和检索图像

时间:2019-06-12 18:13:40

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

我正在尝试上载和检索图像,并遵循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();
                        }
                    });

1 个答案:

答案 0 :(得分:0)

返回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字符串,然后再使用PicassoGlide来在任何ImageView中显示该图像。