如何使用其 downloadURL 從 FirebaseStorage 下載特定檔案?
(例如https://firebasestorage.googleapis.com/v0/b/smart-edubox-90c5d.appspot.com/o/uploaded_files/pdf_files/08-11-2022_15:43:02_dummy.pdf?alt=media&token=f68e7403- 0cb7-4c86-9799-1c47fffd9cb5 )
我將影像、pdf 和文本檔案加載到 RecyclerView 中。當我點擊 CardView(在 RecyclerView 中)時,我將被重定向到瀏覽器中檔案的相應下載 url。我應該如何做到這一點,而不是在網路瀏覽器中打開它,而是將檔案下載到用戶手機中的特定檔案夾中。
任何幫助表示贊賞。謝謝!
uj5u.com熱心網友回復:
要從 URL 下載任何檔案,您只需使用 Android 的下載管理器,這是從 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/pdf_files/08-11-2022_15:43:02_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);
}
這是存盤權限
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;
}
你的清單權限將是
<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" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529336.html
標籤:爪哇安卓
上一篇:OracleSQL-將列的資料默認值更改為NULL-NULL和(null)之間有什么區別嗎?
下一篇:如何從協程回傳值/等待協程完成
