在Firebase存储中上传PDF文件

时间:2016-12-07 13:17:07

标签: android file firebase firebase-storage

我想在我的Android应用中使用Google Firebase下载PPT和PDF。是否可以将PPT和PDF文件上传到Firebase存储,以便用户以后可以下载它们。

3 个答案:

答案 0 :(得分:2)

我之前没有尝试过上传PDF文件,但这应该适用于几乎所有文件类型:

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);
    }
});

如果它不起作用,请告诉我。

答案 1 :(得分:2)

请试试这个

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 :(得分:0)

有任何困难让我知道。

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