Hello Android lovers, In here I am going to discuss “How to download files firebase to Android device“. That means we are going to add a download button to your android application and when it click your file will download to your device.

How to download files Firebase to Android device
First of all in your android xml file, create a design to load the image and a button to download. Like this.
<RelativeLayout
android:id="@+id/image01"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img01"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/btnDownload"
android:text= "Download"
android:layout_width="match_parent"
android:layout_height="50dp"/>
</RelativeLayout
In this xml file, image will display and fetch on the ImageView. Lets create the Java file.
In your Java file, first add properties.
private ImageView img01,
private Button btnDownload;
Then define in the onCreate() method.
img01 = findViewById(R.id.img01);
btnDownload = findViewById(R.id.btnDownload );
We are going to develop download option when the button click. So we have to implement the setOnClickListener method.
#Step 02 setOnClickListener method – How to download files Firebase to Android device
We have to implement in the onClick() method. In here I am going to create another method download option. So follow the below code to get an idea about how to implement.
public void downloadFile(Context context, String fileName, String fileExtension, String destinatioinDirectory, String url){
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context,destinatioinDirectory,"Ganesha-"+fileName+fileExtension);
downloadManager.enqueue(request);
}
After that I am going to add that method to the setOnClickListener. Like this.
btnDownload .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ImagesActivity.this,"Downloading...",Toast.LENGTH_LONG).show();
storageReference = firebaseStorage.getInstance().getReferenceFromUrl("gs://shree-ganesh-78dbe.appspot.com/Images").child("m3.jpg");
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();
downloadFile(ImagesActivity.this,"m3", ".jpg","AndroidTest" , url );
}
});
}
});
Let me explain this method.
- In my Firebase storage, I have a image called m3.jpg.
- Toast means, it is a Toast message when displaying when downloading the file.
- StorageReference is for set the Firebase instance URL to get the image. You can see getReferenceFromUrl() method is here to set the URL and for the child() method, we are going to add the image name and the format.
- storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() this is the method. We are implementing the code in the onSuccess method.
- After that we are passing attributes to the downloadFile() method.
Fetch Image to ImageView – #Step 03 in How to download files Firebase to Android device
Create another method to implement the fetch image option.
private void loadImage(){
storageReference = firebaseStorage.getInstance().getReferenceFromUrl("gs://shree-ganesh-78dbe.appspot.com/Images").child("copy.jpg");
try {
final File locaFile = File.createTempFile("image", "jpg");
storageReference.getFile(locaFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Bitmap bitmap = BitmapFactory.decodeFile(locaFile.getAbsolutePath());
img01.setImageBitmap(bitmap);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
In the onSuccess() method we are going to set the file to Bitmap.
After that you can run the app and you can see the output.
Thank you. Make sure to leave a comment.
If you are a newbie to Android. Follow and refer articles. Click below link to get started.
- Mobile Application Design and Development
- Data Handling in Mobile Application Development
- XML Layout – UI design for android
- How to Create Welcome Screen in Android Studio
- How to create bottom Navigation Bar in Android
- Android OTP verification using firebase
- Adding Share Button – Android Studio
- How to create Music Player with Firebase
Other articles from different categories.
- Android Studio Articles – https://builditmasters.com/category/android-studio/
- Android Studio Firebase Tutorial – https://builditmasters.com/category/android-studio-firebase-tutorial/
- C Programming – https://builditmasters.com/category/programming/
- Flutter – https://builditmasters.com/category/flutter/
- GitHub Tutorials – https://builditmasters.com/category/github/
- Java Programming – https://builditmasters.com/category/java-programming/
- MERN / MEVN Stacks – https://builditmasters.com/category/mern_mevn_stacks/
- Tech News – https://builditmasters.com/category/tech-news/
- Theory Lessons – https://builditmasters.com/category/theory-lessons/
- Adobe Tutorials – https://builditmasters.com/category/adobe-tutorials/
- Best Website for Programming – https://builditmasters.com/category/best-website-for-programming/
- Different Programming Styles – https://builditmasters.com/category/different-programming-styles/
- Earn Money – https://builditmasters.com/category/earn-money/
- Social Word – https://builditmasters.com/category/social-world/