我想在我的安卓应用中使用谷歌Firebase来下载PPT和PDF文件。是否可以将PPT和PDF文件上传到Firebase存储,以便用户稍后下载它们。
我想在我的安卓应用中使用谷歌Firebase来下载PPT和PDF文件。是否可以将PPT和PDF文件上传到Firebase存储,以便用户稍后下载它们。
请试一下这个
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();
}
});
我以前没有尝试过上传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);
}
});
有任何困难请告诉我。
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();
}
});