com.google.firebase.storage.OnProgressListener Java Examples

The following examples show how to use com.google.firebase.storage.OnProgressListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MyUploadService.java    From quickstart-android with Apache License 2.0 4 votes vote down vote up
private void uploadFromUri(final Uri fileUri) {
    Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());

    // [START_EXCLUDE]
    taskStarted();
    showProgressNotification(getString(R.string.progress_uploading), 0, 0);
    // [END_EXCLUDE]

    // [START get_child_ref]
    // Get a reference to store file at photos/<FILENAME>.jpg
    final StorageReference photoRef = mStorageRef.child("photos")
            .child(fileUri.getLastPathSegment());
    // [END get_child_ref]

    // Upload file to Firebase Storage
    Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath());
    photoRef.putFile(fileUri).
            addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                    showProgressNotification(getString(R.string.progress_uploading),
                            taskSnapshot.getBytesTransferred(),
                            taskSnapshot.getTotalByteCount());
                }
            })
            .continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                    // Forward any exceptions
                    if (!task.isSuccessful()) {
                        throw task.getException();
                    }

                    Log.d(TAG, "uploadFromUri: upload success");

                    // Request the public download URL
                    return photoRef.getDownloadUrl();
                }
            })
            .addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(@NonNull Uri downloadUri) {
                    // Upload succeeded
                    Log.d(TAG, "uploadFromUri: getDownloadUri success");

                    // [START_EXCLUDE]
                    broadcastUploadFinished(downloadUri, fileUri);
                    showUploadFinishedNotification(downloadUri, fileUri);
                    taskCompleted();
                    // [END_EXCLUDE]
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // Upload failed
                    Log.w(TAG, "uploadFromUri:onFailure", exception);

                    // [START_EXCLUDE]
                    broadcastUploadFinished(null, fileUri);
                    showUploadFinishedNotification(null, fileUri);
                    taskCompleted();
                    // [END_EXCLUDE]
                }
            });
}