下面的代码运行正常。它从firebase存储中下载pdf文件并将其保存在设备内部存储中。这里似乎遇到的麻烦是我在Android设备监视器中找不到下载的文件。
btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String magazineUrl= magazineUri[0];
new Thread(new Runnable(){
@Override
public void run() {
try {
FirebaseStorage storage = FirebaseStorage.getInstance();
// Creating a reference to the link
StorageReference httpsReference = storage.getReferenceFromUrl(magazineUrl);
File file;
file=new File(mContext.getFilesDir(),"DownloadedMagazines.pdf");
httpsReference.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
System.out.println("It has been downloaded");
// Local temp file has been created
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
System.out.println("It has failed to download");
}
});
}catch (final Exception e) {
System.out.println("Error : Please check your internet connection " + e);
}
}
}).start();
}
});
我的问题是,文件存储在哪里?有没有办法检索下载文件的路径?
答案 0 :(得分:0)
您的文件存储在应用程序的私人目录中。 如果要将文件保存在公共目录中,请使用:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); // For example
File file = new File(path, "DownloadedMagazines.pdf");
您可以使用Environment.DIRECTORY_DOWNLOADS
或Environment.DIRECTORY_DOCUMENTS
以及其他人......
注意,您需要WRITE_EXTERNAL_STORAGE
权限,并且从KITKAT开始,读取权限需要READ_EXTERNAL_STORAGE