Uploading PDF file in Firebase Storage

18,351

Solution 1

Please try this

Uri file = Uri.fromFile(new File("path/to/doc/rivers.pdf"));
StorageReference riversRef = storageRef.child("doc/"+file.getLastPathSegment());
uploadTask = riversRef.putFile(file);

// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle unsuccessful uploads
    }
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
        Uri downloadUrl = taskSnapshot.getDownloadUrl();
    }
});

Solution 2

I haven't tried uploading a PDF file before, but this should work for pretty much all file types:

FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://your-app-name.com");

Uri file = Uri.fromFile(new File("data/data/file-path/file-name"));
Log.d("file", file.getPath());


StorageReference riversRef = storageRef.child("firebase-storage");

UploadTask uploadTask = riversRef.putFile(file);

// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle unsuccessful uploads
        Log.d("uploadFail", "" + exception);

    }
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
        sendNotification("upload backup", 1);

        Uri downloadUrl = taskSnapshot.getDownloadUrl();

        Log.d("downloadUrl", "" + downloadUrl);
    }
});

if it doesn't work for you let me know.

Share:
18,351
Mayank Aggarwal
Author by

Mayank Aggarwal

Updated on June 04, 2022

Comments

  • Mayank Aggarwal
    Mayank Aggarwal about 2 years

    I want to use Google Firebase for my android app to download PPTs and PDFs. Is it possible to upload PPT and PDF files to Firebase storage so that users can download them later.