如何从从Firebase存储下载的文件中获取本地路径 [英] How to get the local path from a file downloaded from Firebase Storage
问题描述
我对Firebase文档有点困惑: https://firebase.google.com/docs/storage/android/download-files
I am a bit confused with the Firebase documentation: https://firebase.google.com/docs/storage/android/download-files
我试图通过文件的URL从Firebase下载文件,然后获取其本地路径:
I am trying to download a file from Firebase through the URL of the file and then get its local path:
mStorageReference.getFile(downloadURL).addOnSuccessListener(new
OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
// I assume the file is now downloaded from the given URL and is on the device
// HOW DO I GET THE PATH TO THE FILE ON THE DEVICE ?
}
});
问题在评论中.
推荐答案
从文档开始:-
getFile()方法直接将文件下载到本地设备.
The getFile() method downloads a file directly to a local device.
因此,您可以先创建一个临时文件,而不是执行您正在做的事情.以下是一个示例:-
So, instead of doing what you're doing, you can first create a temporary file. Following is an example :-
File localFile = File.createTempFile("images", "jpg");
此后,您将此 localFile
作为参数传递给您的 getFile()
方法(而不是传递 downloadURL
).因此,当您的 onSuccess()
被触发时,此文件将填充有已下载的数据,您可以根据需要访问它.像这样的东西:-
After that, you pass this localFile
as a parameter to your getFile()
method ( instead of passing downloadURL
). So, when your onSuccess()
is fired, this file is populated with the data that has been downloaded and you can access it for whatever you need. Something like this :-
mStorageReference.getFile(localFile).addOnSuccessListener(new
OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
//localFile contains your downloaded data
}
});
请注意,在此示例中, localFile
是临时的,但是您也可以在指定的路径上创建一个File.这取决于您的用例.
Note that in this example, the localFile
is temporary, but you can create a File at your specified path too. It depends on your use case.
这篇关于如何从从Firebase存储下载的文件中获取本地路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!