How to download file into internal downloads folder in Android - 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");
}
});

Related

How to download image file from firebase storage

okay so the method I have tried is .getfile() for downloading the image. it triggers onsuccess listener as well but I can't locate the file in my internal storage, not sure if I can access it
#Override
public void onDownloadClick(int position) {
Toast.makeText(this, "will try to dwonload XD" , Toast.LENGTH_LONG).show();
Upload selectedItem= mUploads.get(position);
final String selectedkey= selectedItem.getKey();
StorageReference imgRef=mStorage.getReferenceFromUrl(selectedItem.getImageUrl());
try{
File localFIle=File.createTempFile("images", "jpg");
imgRef.getFile(localFIle).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(ImagesActivity.this, "downloaded" , Toast.LENGTH_LONG).show();
Log.d("file ", "filedownloaded");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(ImagesActivity.this, "not able to download" , Toast.LENGTH_LONG).show();
}
});
}
catch (Exception e)
{
}
}
It looks like you're creating a File object called localFIle. If you want to know what path that it, just log its string value. If that's not the location you want, try something else. I suggest reading the documentation on locating files on Android devices.

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());
}
});

Download audios from Firebase (Android Studio)

people of StackOverflow.
Recently I finish a course of Android Studio andd in classes we begin to work with Firebase. I managed that my application reproduce audios in mp3 from the database, but now I would like to share it in another application, for it, I have done this:
this.stRef = FirebaseStorage.getInstance().getReference().child("audios/audioname.mp3");
File localFile = null;
String downloadedaudio="";
try {
localFile = File.createTempFile("audioname", ".mp3");
downloadedaudio = localFile.getPath();
} catch (IOException e) {
e.printStackTrace();
}
stRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getContext(),"Downloaded",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(getContext(),"Failed to Download"+exception,Toast.LENGTH_LONG).show();
}
});
The app download a temp file, i check it in Android Studio file explorer, but then i does not share anything:
Uri uri = Uri.parse(downloadedaudio);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/mp3");
share.putExtra(Intent.EXTRA_STREAM,uri);
c.startActivity(Intent.createChooser(share, "Compartir sonido en: "));
}
Any idea of what i am doing wrong?
Thank you.

How to download all Files of FirebaseStorage folder on Android?

I wants to download all content of a Folder in FirebaseStorage. I can download a only file (image) like this:
FirebaseStorage storage = FirebaseStorage.getInstance();
try {
final File localFile = File.createTempFile("ImagensExercicios","bmp");
storage.getReference().child("ImagensExercicios").child("abdominal_1.bmp").getFile(localFile).
addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.i(TAG,"onSuccess()");
Bitmap bitmap;
bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
imageView.setImageBitmap(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.i(TAG,"onFailure() " + e.getMessage());
}
});
} catch (Exception e) {
e.printStackTrace();
}
This works, but I wants download all images of the folder. Exists a way to do this?
In the firebase folder rename the items as
abdominal_1.bmp
abdominal_2.bmp
abdominal_3.bmp
And use above try catch inside a forloop.

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