将文件上传到Google Cloud Firestore和从Google Cloud Firestore下载文件 [英] Uploading and downloading files to/from Google Cloud Firestore

查看:80
本文介绍了将文件上传到Google Cloud Firestore和从Google Cloud Firestore下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到Firebase已经发布了一个新的Beta版本,称为Cloud Firestore.在文档中,对文档的所有操作都进行了很好的描述,但是我无法找到有关使用Android将媒体文件上传和下载到Cloud Firestore中的任何信息...

I see that the Firebase has already a new beta version released, called Cloud Firestore. In the documentation all operations with the documents are described very well, but I am not able to find anything about uploading and downloading media files into the Cloud Firestore using Android...

是否有人可以上载/下载媒体文件(例如mp3文件和图像)的任何信息/教程等?

Does anyone has any information/tutorial etc for uploading/downloading media files (for example mp3 files and images)?

非常感谢您的回答!

推荐答案

您不能将文件存储到Firebase Cloud Firestore中,而是可以结合使用Firebase Storage和Firebase Cloud Firestore来激活所需的功能.

You can't store files to Firebase Cloud Firestore instead you can use the combination of Firebase Storage and Firebase Cloud Firestore to active the desired functionality.

Firebase存储用于存储文件并从中下载.

Firebase Storage is to storage files and download from it.

Firebase实时数据库用于在其上存储json no-sql数据库.

Firebase Realtime Database is to store json no-sql database on it.

Firebase Cloud Firestore 是Firebase实时数据库的高级版本,与Realtime数据库的区别在于它是基于文档的非sql数据库.

Firebase Cloud Firestore is advanced version of Firebase realtime database the difference from Realtime database is that it is Document based non-sql database.

假设您需要使用数据库和存储来开发应用程序,则需要将任何数据库与Firebase Storage结合使用.将文件存储在Firebase存储中,并将其网址保存在Firebase实时或Firebase Cloud Firestore中,以供下载和上传.

Suppose you need to develop an application with database and storage you need combination of any of Database with Firebase Storage. Store files in firebase storage and save their urls in firebase realtime or firebase cloud firestore for downloading and uploading them.

要在Firebase存储器上上传文件:

To Upload file on firebase storage :

FirebaseStorage firebaseStorage;
            //for firebase storage
            firebaseStorage = FirebaseStorage.getInstance();
            StorageReference storageReference;
            storageReference = firebaseStorage.getReferenceFromUrl("url");
            final StorageReference imageFolder = storageReference.child(""+imageName);
            imageFolder.putFile(saveUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    //submitted sucessfully
                    imageFolder.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {

                            Log.wtf(TAG, "download image path : "+uri.toString() );
                          //now you have path to the uploaded file save this path to your database
                          uploadDataToUserUploadedImage(uri);

                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {

                            getMvpView().stopProgressLoading();
                            getMvpView().onError("Fail to submit feedback "+e.getMessage());
                            getMvpView().hideLoading();
                            return;
                        }
                    });
                }
            }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                    double  progress = (100.0 * taskSnapshot.getBytesTransferred()/taskSnapshot.getTotalByteCount());
                    getMvpView().publishProgress((int)progress);
                    Log.d(TAG, "onProgress: "+progress );
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    getMvpView().hideLoading();
                    getMvpView().stopProgressLoading();
                    getMvpView().onError("Error: "+e.getMessage());
                }
            });

这篇关于将文件上传到Google Cloud Firestore和从Google Cloud Firestore下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆