0

How do I download a specific file from the FirebaseStorage using its downloadURL?

(e.g. https://firebasestorage.googleapis.com/v0/b/smart-edubox-90c5d.appspot.com/o/uploaded_files%2Fpdf_files%2F08-11-2022_15%3A43%3A02_dummy.pdf?alt=media&token=f68e7403-0cb7-4c86-9799-1c47fffd9cb5 )

I load the image, pdf, and text file into a RecyclerView. When I tap on a CardView (in the RecyclerView) I will be redirected to the file's corresponding download url in a browser. How should I do it so that instead of opening it in a web browser, the file would be downloaded to a specific folder in the user's phone.

Any help is appreciated. Thanks!

1 Answer 1

1

To download any file from Url you can simply use the Download Manager of Android this is code to download file from URL

String[] PERMISSIONS = { android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE, }; if (!hasPermissions(activity.this, PERMISSIONS)) { String url = "https://firebasestorage.googleapis.com/v0/b/smart-edubox-90c5d.appspot.com/o/uploaded_files%2Fpdf_files%2F08-11-2022_15%3A43%3A02_dummy.pdf?alt=media&token=f68e7403-0cb7-4c86-9799-1c47fffd9cb5"; DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setDescription("Some descrition"); request.setTitle("Some title"); // in order for this if to run, you must use the android 3.2 to compile your app request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.pdf"); // get download service and enqueue file DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(request); } 

this is permission for Storage

public static boolean hasPermissions(Context context, String... permissions) { if (context != null && permissions != null) { for (String permission : permissions) { if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { return false; } } } return true; } 

and your manifest permissions will be

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
Sign up to request clarification or add additional context in comments.

7 Comments

What method should I import for getSystemService in DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); ?
import androidx.core.app.ActivityCompat; import android.app.DownloadManager; import android.content.Context; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.os.Environment;
Oh wait nvm, I forgot to add context before it. I forgot I'm not coding inside an Activity.
this is my import there is nothing specific for getSystemService its default import for android simply ALT+ENTER
i edited my answer after seeing recyclerview in your question
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.