Reputation: 93
The code below works just fine.It downloads a pdf file from firebase storage and saves it in the devices internal storage. What seems to be the trouble here is that I could not find the downloaded file in the Android Device Monitor.
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();
}
});
My questions are, Where is the file stored? Is there a possible way to retrieve the path of the downloaded file?
Upvotes: 1
Views: 1210
Reputation: 1543
Your file is stored in the private directory of your application. If you want to save your file in a public directory, use :
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); // For example
File file = new File(path, "DownloadedMagazines.pdf");
You can use Environment.DIRECTORY_DOWNLOADS
or Environment.DIRECTORY_DOCUMENTS
and others...
Take care, you need the WRITE_EXTERNAL_STORAGE
permission, and starting in KITKAT, read access requires the READ_EXTERNAL_STORAGE
Upvotes: 1