Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading and downloading files to/from Google 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...

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!

like image 986
Gabriella Angelova Avatar asked Dec 03 '22 20:12

Gabriella Angelova


1 Answers

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());
    }
});
like image 151
Nouman Ch Avatar answered Dec 06 '22 11:12

Nouman Ch