Donwloading files from Firebase to Android - android

I have stored some doc files in a folder "Colleges_names" in Firebase storage. I want to download them in my android app. When I click the required button to download file nothing happens.
public void onClick(View view) {
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://du-admissions-app.appspot.com");
StorageReference islandRef = storageRef.child("COLLEGES-NAMES").child("CLG_BA(ENGLISH).docx");
File rootPath = new File(Environment.getExternalStorageDirectory(), "file_name");
if(!rootPath.exists()) {
rootPath.mkdirs();
}
final File localFile = new File(rootPath,"CLG_BA(ENGLISH).docx");
islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.e("firebase ",";local tem file created created " +localFile.toString());
// updateDb(timestamp,localFile.toString(),position);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("firebase ",";local tem file not created created " +exception.toString());
}
});

You can't retrieve those files with their extension, you should upload your files programmatically first, get the downloadUrl() of the file uploaded and then retrieve that downloadUrl() and being the download of the files.
I would recommend you to read this.

Related

How to download multiple files of firebase Storage Android?

I am downloading files from firebase storage
But I can only download one by one
Can I download multiple files at once?
Is it the best way to repeat the same code?
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference();
StorageReference islandRef = storageRef.child(filename);
final String saveFilename = filename;
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/down/");
// If no folders
if (!dir.exists()) {
dir.mkdirs();
}
final File localFile = new File(dir,saveFilename);
islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
}
});
Simply call putFile once for each file you want to download. You will need a different reference and local file for each call.
FileDownloadTask task1 = storageRef1.getFile(localFile1);
FileDownloadTask task2 = storageRef2.getFile(localFile3);
FileDownloadTask task3 = storageRef3.getFile(localFile3);
You can then wait for all them to complete with Tasks.whenAll():
Tasks.whenAll(task1, task2, task3)
.addOnSuccessListener(new OnSuccessListener<List<Task<*>>() {
#Override
public void onSuccess(Task task) {
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
}
});
There is no specific API to download multiple files. You'll just have to download them by calling the same API for each file.

How to download file from Firebase storage to custom folder?

I have been searching all day long even here on stack overflow but none of the code seems to work. I want to download a file(s) from FirebaseStorage to a custom folder on internal/external storage but no luck whatsoever.
my code:
private void downloadfile() {
mStorageRef = FirebaseStorage.getInstance().getReference();
ref = mStorageRef.child("song.mp3");
try {
File localFile = File.createTempFile("song", "mp3");
} catch (IOException e) {
e.printStackTrace();
}
File storagePath = new File(Environment.getExternalStorageDirectory(), "My Folder");
// Create direcorty if not exists
if(!storagePath.exists()) {
storagePath.mkdirs();
}
final File myFile = new File(storagePath,"song.mp3");
mStorageRef.getFile(myFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
// Local temp file has been created
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
}
1.) Create the path to the file
String sdcardPath = Environment.getExternalStorageDirectory().toString();
String filePath = sdCardPath + "<YOUR_PATH_LIKE_/Downloads>" + "Filename";
2.) Create the file:
File directory = new File(filePath);

Download pdf from Firebase with URL

I upload pdf in Storage Firebase with StorageReference and I saved the https link related to the file.
I do not know how to download the file with this link.
Thw flow not enter in addOnSuccessListener. Why?
How can I do ?
StorageReference storageRef = FirebaseStorage.getInstance().getReferenceFromUrl(urlResource);
File localFile = File.createTempFile("example", ".pdf");
storageRef.getFile(localFile)
.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.i("firebase ",";local tem file created created ");
// updateDb(timestamp,localFile.toString(),position);
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.i("firebase ",";local tem file not created created " +exception.toString());
}
});

How to download file into internal downloads folder in Android

I am pretty new with this, and I could use some help.
I am having difficulties downloading files from firebase storage into my android device storage. Problem is that files are downloaded into cache memory of app, and I would like to use them when I'm offline. Other thing is that I am not using SD card, so I would like my files downloaded into some of user accessible folder, if possible into download folder in internal memory.
Here is my code:
StorageReference ref = storage.getReferenceFromUrl("gs://diplomskirad-948a7.appspot.com/Dennis Lloyd - NEVERMIND.mp3");
File localFile=null;
try {
localFile = File.createTempFile("audio", ".mp3");
} catch (IOException e) {
e.printStackTrace();
}
ref.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.d("Success", "File downloaded successfully");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("Error", "Error downloading file");
}
});
I guess should somehow change my "onSuccess" method, but I didn't have any success until now.
Try this:-
StorageReference ref = storage.getReferenceFromUrl("gs://diplomskirad-948a7.appspot.com/Dennis Lloyd - NEVERMIND.mp3");
File path= new File(Environment.getExternalStorageDirectory(), "file_name");
if(!path.exists()) {
path.mkdirs();
}
final File localFile = new File(path,"audio.mp3");
ref.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.d("Success", "File downloaded successfully " + localFile.toString());
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("Error", "Error downloading file");
}
});

In Android, where does the file downloaded from Firebase storage get stored?

I uploaded a file to Firebase Storage from my project console. Now, I downloaded that file from my app using the method 'Download to a local file' as mentioned in the Firebase Storage documentation, but I am not able to find the downloaded file on my phone.
Can someone tell me where does that file get downloaded to?
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
final StorageReference storageReference = firebaseStorage.getReferenceFromUrl("gs://ldq-app-d2e6b.appspot.com");
button_down.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String fileName = new String(editText_file.getText().toString());
StorageReference childReference = storageReference.child("Quizzes");
StorageReference fileReference = storageReference.child("Quizzes/" + fileName + ".pdf");
File localFile = null;
try {
localFile = File.createTempFile(fileName, "pdf");
}
catch (IOException ioe){
Toast.makeText(getContext(), "File creation failed", Toast.LENGTH_SHORT).show();
}
fileReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getContext(),"File downloaded",LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getContext(),"Download failed. Try again!", LENGTH_SHORT).show();
}
});
//Toast.makeText(getContext(),"Button working",LENGTH_SHORT).show();
}
});
As per Firebase Document, they are creating a temporary file for saving Image from Url. Link
File localFile = File.createTempFile("images", "jpg");
If you want to store it on phone's internal memory or in external memory then just create a File with storage path and pass it to FileDownloadTask.
File storagePath = new File(Environment.getExternalStorageDirectory(), "directory_name");
// Create direcorty if not exists
if(!storagePath.exists()) {
storagePath.mkdirs();
}
final File myFile = new File(storagePath,"file_name");
yourStorageRef.getFile(myFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
// Local temp file has been created
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});

Categories

Resources