Uploading and downloading files to/from Google Cloud Firestore

左心房为你撑大大i 提交于 2020-12-29 07:42:18

问题


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...

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

Thank you very much in advance for the answers!


回答1:


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 Storage is to storage files and download from it.

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

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

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.

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());
                }
            });


来源:https://stackoverflow.com/questions/49529957/uploading-and-downloading-files-to-from-google-cloud-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!