3
votes

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.

3
Any errors happening when you try it?OneCricketeer
I haven't tried it yet. Want to know if I can do itMayank Aggarwal
I think trying it would be quicker than waiting for an answerOneCricketeer

3 Answers

3
votes

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

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.

0
votes

Any difficulty let Me Know.

  Uri file = Uri.fromFile(new File(PATH));
    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();
        }
    });