Search in sources :

Example 1 with OnProgressListener

use of com.google.firebase.storage.OnProgressListener in project duckFood by sanjaytharagesh31.

the class Main2Activity method uploadFile.

// this method is uploading the file
// the code is same as the previous tutorial
// so we are not explaining it
private void uploadFile(Uri data, int r) {
    if (r == PICK_PDF_CODE) {
        progressBar.setVisibility(View.VISIBLE);
        StorageReference sRef = mStorageReference.child("/PDF Uploads/" + editTextFilename.getText().toString() + ".pdf");
        sRef.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @SuppressWarnings("VisibleForTests")
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressBar.setVisibility(View.GONE);
                textViewStatus.setText("PDF File Uploaded Successfully");
                Upload upload = new Upload(editTextFilename.getText().toString(), taskSnapshot.getDownloadUrl().toString());
                mDatabaseReference.child(mDatabaseReference.push().getKey()).setValue(upload);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

            @SuppressWarnings("VisibleForTests")
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                textViewStatus.setText((int) progress + "% Uploading...");
            }
        });
    } else if (r == PICK_VIDEO_CODE) {
        progressBar.setVisibility(View.VISIBLE);
        StorageReference mp4ref = videoStorageRefernce.child("/MP4 Uploads/" + editTextFilename.getText().toString() + ".mp4");
        mp4ref.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @SuppressWarnings("VisibleForTests")
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressBar.setVisibility(View.GONE);
                textViewStatus.setText("Video File Uploaded Successfully");
                Upload upload = new Upload(editTextFilename.getText().toString(), taskSnapshot.getDownloadUrl().toString());
                videoDatabaseRefernce.child(videoDatabaseRefernce.push().getKey()).setValue(upload);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

            // Important Code to be added
            @SuppressWarnings("VisibleForTests")
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                textViewStatus.setText((int) progress + "% Uploading...");
            }
        });
    } else if (r == PICK_TEXT_CODE) {
        progressBar.setVisibility(View.VISIBLE);
        StorageReference txtref = textStorageReference.child("/Text Uploads/" + editTextFilename.getText().toString() + ".txt");
        txtref.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @SuppressWarnings("VisibleForTests")
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressBar.setVisibility(View.GONE);
                textViewStatus.setText("Text File Uploaded Successfully");
                Upload upload = new Upload(editTextFilename.getText().toString(), taskSnapshot.getDownloadUrl().toString());
                textDatabaseReference.child(textDatabaseReference.push().getKey()).setValue(upload);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

            // Important Code to be added
            @SuppressWarnings("VisibleForTests")
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                textViewStatus.setText((int) progress + "% Uploading...");
            }
        });
    } else if (r == PICK_AUDIO_CODE) {
        progressBar.setVisibility(View.VISIBLE);
        StorageReference audioref = textStorageReference.child("/Audio Uploads/" + editTextFilename.getText().toString() + ".txt");
        audioref.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @SuppressWarnings("VisibleForTests")
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressBar.setVisibility(View.GONE);
                textViewStatus.setText("Audio File Uploaded Successfully");
                Upload upload = new Upload(editTextFilename.getText().toString(), taskSnapshot.getDownloadUrl().toString());
                audioDatabaseReference.child(audioDatabaseReference.push().getKey()).setValue(upload);
            }
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

            // Important Code to be added
            @SuppressWarnings("VisibleForTests")
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                textViewStatus.setText((int) progress + "% Uploading...");
            }
        });
    }
}
Also used : StorageReference(com.google.firebase.storage.StorageReference) UploadTask(com.google.firebase.storage.UploadTask) NonNull(android.support.annotation.NonNull) OnProgressListener(com.google.firebase.storage.OnProgressListener) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 2 with OnProgressListener

use of com.google.firebase.storage.OnProgressListener in project quickstart-android by firebase.

the class MyUploadService method uploadFromUri.

// [START upload_from_uri]
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]
        }
    });
}
Also used : Task(com.google.android.gms.tasks.Task) UploadTask(com.google.firebase.storage.UploadTask) StorageReference(com.google.firebase.storage.StorageReference) Uri(android.net.Uri) UploadTask(com.google.firebase.storage.UploadTask) OnProgressListener(com.google.firebase.storage.OnProgressListener) NonNull(androidx.annotation.NonNull) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 3 with OnProgressListener

use of com.google.firebase.storage.OnProgressListener in project duckFood by sanjaytharagesh31.

the class OrgDataUpload method uploadFile.

private void uploadFile(Uri data, int r) {
    if (r == PICK_IMG_CODE) {
        if (image.getText().toString().length() != 0) {
            StorageReference sRef = mStorageReference.child("/Organizations/Images/" + image.getText().toString() + ".jpg");
            sRef.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

                @SuppressWarnings("VisibleForTests")
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(getApplicationContext(), "Image Uploaded Successfully", Toast.LENGTH_SHORT).show();
                    imgurl = taskSnapshot.getDownloadUrl().toString();
                }
            }).addOnFailureListener(new OnFailureListener() {

                @Override
                public void onFailure(@NonNull Exception exception) {
                    Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                }
            }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

                @SuppressWarnings("VisibleForTests")
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(getApplicationContext(), "Uploading Organization Image", Toast.LENGTH_SHORT).show();
                }
            });
        } else
            Toast.makeText(getApplicationContext(), "Please Enter Image Name", Toast.LENGTH_SHORT).show();
    } else if (r == PICK_PDF_CODE) {
        if (cert.getText().toString().length() != 0) {
            StorageReference sRef = mStorageReference.child("/Organizations/PDF Certificates/" + cert.getText().toString() + ".pdf");
            sRef.putFile(data).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

                @SuppressWarnings("VisibleForTests")
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(getApplicationContext(), "Certificate Uploaded Successfully", Toast.LENGTH_SHORT).show();
                    certurl = taskSnapshot.getDownloadUrl().toString();
                }
            }).addOnFailureListener(new OnFailureListener() {

                @Override
                public void onFailure(@NonNull Exception exception) {
                    Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                }
            }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

                @SuppressWarnings("VisibleForTests")
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(getApplicationContext(), "Uploading Organization Certificate", Toast.LENGTH_SHORT).show();
                }
            });
        } else
            Toast.makeText(getApplicationContext(), "Please Enter Certificate Name", Toast.LENGTH_SHORT).show();
    }
}
Also used : StorageReference(com.google.firebase.storage.StorageReference) UploadTask(com.google.firebase.storage.UploadTask) NonNull(android.support.annotation.NonNull) OnProgressListener(com.google.firebase.storage.OnProgressListener) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Aggregations

OnFailureListener (com.google.android.gms.tasks.OnFailureListener)3 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)3 OnProgressListener (com.google.firebase.storage.OnProgressListener)3 StorageReference (com.google.firebase.storage.StorageReference)3 UploadTask (com.google.firebase.storage.UploadTask)3 NonNull (android.support.annotation.NonNull)2 Uri (android.net.Uri)1 NonNull (androidx.annotation.NonNull)1 Task (com.google.android.gms.tasks.Task)1