Cloud Storage for Firebase به شما این امکان را می دهد که به سرعت و به راحتی فایل ها را از یک سطل Cloud Storage که توسط Firebase تهیه و مدیریت می شود دانلود کنید.
یک مرجع ایجاد کنید
gs://
یا https://
موجود ایجاد کنید که به یک شی در Cloud Storage ارجاع میدهد.
// Create a storage reference from our app val storageRef = storage.reference // Create a reference with an initial file path and name val pathReference = storageRef.child("images/stars.jpg") // Create a reference to a file from a Google Cloud Storage URI val gsReference = storage.getReferenceFromUrl("gs://bucket/images/stars.jpg") // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! val httpsReference = storage.getReferenceFromUrl( "https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg", )
// Create a storage reference from our app StorageReference storageRef = storage.getReference(); // Create a reference with an initial file path and name StorageReference pathReference = storageRef.child("images/stars.jpg"); // Create a reference to a file from a Google Cloud Storage URI StorageReference gsReference = storage.getReferenceFromUrl("gs://bucket/images/stars.jpg"); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! StorageReference httpsReference = storage.getReferenceFromUrl("https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");
دانلود فایل ها
getBytes()
یا getStream()
فایل ها را از Cloud Storage دانلود کنید. اگر ترجیح می دهید فایل را با کتابخانه دیگری دانلود کنید، می توانید یک URL دانلود با getDownloadUrl()
دریافت کنید.
دانلود در حافظه
getBytes()
فایل را در byte[]
دانلود کنید. این ساده ترین راه برای دانلود یک فایل است، اما باید تمام محتویات فایل شما را در حافظه بارگیری کند. اگر فایلی بزرگتر از حافظه موجود برنامه خود درخواست کنید، برنامه شما خراب می شود. برای محافظت در برابر مشکلات حافظه، getBytes()
حداکثر مقدار بایت را برای دانلود نیاز دارد. حداکثر اندازه را روی چیزی تنظیم کنید که میدانید برنامهتان از پس آن بر میآید، یا از روش دانلود دیگری استفاده کنید.
var islandRef = storageRef.child("images/island.jpg") val ONE_MEGABYTE: Long = 1024 * 1024 islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener { // Data for "images/island.jpg" is returned, use this as needed }.addOnFailureListener { // Handle any errors }
StorageReference islandRef = storageRef.child("images/island.jpg"); final long ONE_MEGABYTE = 1024 * 1024; islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() { @Override public void onSuccess(byte[] bytes) { // Data for "images/island.jpg" is returns, use this as needed } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
دانلود به یک فایل محلی
getFile()
یک فایل را مستقیماً در یک دستگاه محلی دانلود می کند. اگر کاربران شما میخواهند در حالت آفلاین به فایل دسترسی داشته باشند یا فایل را در برنامه دیگری به اشتراک بگذارند، از این استفاده کنید. getFile()
یک DownloadTask
برمی گرداند که می توانید از آن برای مدیریت دانلود و نظارت بر وضعیت دانلود استفاده کنید.
islandRef = storageRef.child("images/island.jpg") val localFile = File.createTempFile("images", "jpg") islandRef.getFile(localFile).addOnSuccessListener { // Local temp file has been created }.addOnFailureListener { // Handle any errors }
islandRef = storageRef.child("images/island.jpg"); File localFile = File.createTempFile("images", "jpg"); islandRef.getFile(localFile).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 } });
دانلود داده ها از طریق URL
getDownloadUrl()
در مرجع Cloud Storage دریافت کنید.
storageRef.child("users/me/profile.png").downloadUrl.addOnSuccessListener { // Got the download URL for 'users/me/profile.png' }.addOnFailureListener { // Handle any errors }
storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { // Got the download URL for 'users/me/profile.png' } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
دانلود تصاویر با FirebaseUI
app/build.gradle
خود اضافه کنید:
dependencies { // FirebaseUI Storage only implementation 'com.firebaseui:firebase-ui-storage:7.2.0' }
سپس می توانید تصاویر را مستقیماً از Cloud Storage در ImageView
بارگیری کنید:
// Reference to an image file in Cloud Storage val storageReference = Firebase.storage.reference // ImageView in your Activity val imageView = findViewById<ImageView>(R.id.imageView) // Download directly from StorageReference using Glide // (See MyAppGlideModule for Loader registration) Glide.with(context) .load(storageReference) .into(imageView)
// Reference to an image file in Cloud Storage StorageReference storageReference = FirebaseStorage.getInstance().getReference(); // ImageView in your Activity ImageView imageView = findViewById(R.id.imageView); // Download directly from StorageReference using Glide // (See MyAppGlideModule for Loader registration) Glide.with(context) .load(storageReference) .into(imageView);
مدیریت تغییرات چرخه حیات فعالیت
دانلودها در پسزمینه ادامه مییابند حتی پس از تغییر چرخه عمر فعالیت (مانند ارائه یک گفتگو یا چرخاندن صفحه). هر شنونده ای که پیوست کرده بودید نیز پیوست خواهد ماند. اگر پس از توقف فعالیت با آنها تماس گرفته شود، این می تواند نتایج غیرمنتظره ای ایجاد کند.
می توانید این مشکل را با اشتراک شنوندگان خود با یک محدوده فعالیت حل کنید تا پس از توقف فعالیت، به طور خودکار آنها را لغو ثبت کنید. سپس، هنگام شروع مجدد فعالیت، از روش getActiveDownloadTasks
استفاده کنید تا کارهای دانلودی را که هنوز در حال اجرا هستند یا اخیراً تکمیل شده اند، بدست آورید.
مثال زیر این را نشان میدهد و همچنین نشان میدهد که چگونه میتوان مسیر مرجع ذخیرهسازی مورد استفاده را ادامه داد.
override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) // If there's a download in progress, save the reference so you can query it later outState.putString("reference", storageRef.toString()) } override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) // If there was a download in progress, get its reference and create a new StorageReference val stringRef = savedInstanceState.getString("reference") ?: return storageRef = Firebase.storage.getReferenceFromUrl(stringRef) // Find all DownloadTasks under this StorageReference (in this example, there should be one) val tasks = storageRef.activeDownloadTasks if (tasks.size > 0) { // Get the task monitoring the download val task = tasks[0] // Add new listeners to the task using an Activity scope task.addOnSuccessListener(this) { // Success! // ... } } }
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // If there's a download in progress, save the reference so you can query it later if (mStorageRef != null) { outState.putString("reference", mStorageRef.toString()); } } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // If there was a download in progress, get its reference and create a new StorageReference final String stringRef = savedInstanceState.getString("reference"); if (stringRef == null) { return; } mStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl(stringRef); // Find all DownloadTasks under this StorageReference (in this example, there should be one) List<FileDownloadTask> tasks = mStorageRef.getActiveDownloadTasks(); if (tasks.size() > 0) { // Get the task monitoring the download FileDownloadTask task = tasks.get(0); // Add new listeners to the task using an Activity scope task.addOnSuccessListener(this, new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { @Override public void onSuccess(FileDownloadTask.TaskSnapshot state) { // Success! // ... } }); } }
رسیدگی به خطاها
دلایل متعددی وجود دارد که ممکن است هنگام دانلود خطا رخ دهد، از جمله اینکه فایل موجود نیست یا کاربر اجازه دسترسی به فایل مورد نظر را ندارد. اطلاعات بیشتر در مورد خطاها را می توانید در بخش Handle Errors در اسناد پیدا کنید.
مثال کامل
یک مثال کامل از دانلود با مدیریت خطا در زیر نشان داده شده است:
storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener { // Use the bytes to display the image }.addOnFailureListener { // Handle any errors }
storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() { @Override public void onSuccess(byte[] bytes) { // Use the bytes to display the image } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
همچنین میتوانید فرادادههایی را برای فایلهایی که در Cloud Storage ذخیره میشوند دریافت و بهروزرسانی کنید .
،Cloud Storage for Firebase به شما این امکان را می دهد که به سرعت و به راحتی فایل ها را از یک سطل Cloud Storage که توسط Firebase تهیه و مدیریت می شود دانلود کنید.
یک مرجع ایجاد کنید
gs://
یا https://
موجود ایجاد کنید که به یک شی در Cloud Storage ارجاع میدهد.
// Create a storage reference from our app val storageRef = storage.reference // Create a reference with an initial file path and name val pathReference = storageRef.child("images/stars.jpg") // Create a reference to a file from a Google Cloud Storage URI val gsReference = storage.getReferenceFromUrl("gs://bucket/images/stars.jpg") // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! val httpsReference = storage.getReferenceFromUrl( "https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg", )
// Create a storage reference from our app StorageReference storageRef = storage.getReference(); // Create a reference with an initial file path and name StorageReference pathReference = storageRef.child("images/stars.jpg"); // Create a reference to a file from a Google Cloud Storage URI StorageReference gsReference = storage.getReferenceFromUrl("gs://bucket/images/stars.jpg"); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! StorageReference httpsReference = storage.getReferenceFromUrl("https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");
دانلود فایل ها
getBytes()
یا getStream()
فایل ها را از Cloud Storage دانلود کنید. اگر ترجیح می دهید فایل را با کتابخانه دیگری دانلود کنید، می توانید یک URL دانلود با getDownloadUrl()
دریافت کنید.
دانلود در حافظه
getBytes()
فایل را در byte[]
دانلود کنید. این ساده ترین راه برای دانلود یک فایل است، اما باید تمام محتویات فایل شما را در حافظه بارگیری کند. اگر فایلی بزرگتر از حافظه موجود برنامه خود درخواست کنید، برنامه شما خراب می شود. برای محافظت در برابر مشکلات حافظه، getBytes()
حداکثر مقدار بایت را برای دانلود نیاز دارد. حداکثر اندازه را روی چیزی تنظیم کنید که میدانید برنامهتان از پس آن بر میآید، یا از روش دانلود دیگری استفاده کنید.
var islandRef = storageRef.child("images/island.jpg") val ONE_MEGABYTE: Long = 1024 * 1024 islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener { // Data for "images/island.jpg" is returned, use this as needed }.addOnFailureListener { // Handle any errors }
StorageReference islandRef = storageRef.child("images/island.jpg"); final long ONE_MEGABYTE = 1024 * 1024; islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() { @Override public void onSuccess(byte[] bytes) { // Data for "images/island.jpg" is returns, use this as needed } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
دانلود به یک فایل محلی
getFile()
یک فایل را مستقیماً در یک دستگاه محلی دانلود می کند. اگر کاربران شما میخواهند در حالت آفلاین به فایل دسترسی داشته باشند یا فایل را در برنامه دیگری به اشتراک بگذارند، از این استفاده کنید. getFile()
یک DownloadTask
برمی گرداند که می توانید از آن برای مدیریت دانلود و نظارت بر وضعیت دانلود استفاده کنید.
islandRef = storageRef.child("images/island.jpg") val localFile = File.createTempFile("images", "jpg") islandRef.getFile(localFile).addOnSuccessListener { // Local temp file has been created }.addOnFailureListener { // Handle any errors }
islandRef = storageRef.child("images/island.jpg"); File localFile = File.createTempFile("images", "jpg"); islandRef.getFile(localFile).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 } });
دانلود داده ها از طریق URL
getDownloadUrl()
در مرجع Cloud Storage دریافت کنید.
storageRef.child("users/me/profile.png").downloadUrl.addOnSuccessListener { // Got the download URL for 'users/me/profile.png' }.addOnFailureListener { // Handle any errors }
storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { // Got the download URL for 'users/me/profile.png' } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
دانلود تصاویر با FirebaseUI
app/build.gradle
خود اضافه کنید:
dependencies { // FirebaseUI Storage only implementation 'com.firebaseui:firebase-ui-storage:7.2.0' }
سپس می توانید تصاویر را مستقیماً از Cloud Storage در ImageView
بارگیری کنید:
// Reference to an image file in Cloud Storage val storageReference = Firebase.storage.reference // ImageView in your Activity val imageView = findViewById<ImageView>(R.id.imageView) // Download directly from StorageReference using Glide // (See MyAppGlideModule for Loader registration) Glide.with(context) .load(storageReference) .into(imageView)
// Reference to an image file in Cloud Storage StorageReference storageReference = FirebaseStorage.getInstance().getReference(); // ImageView in your Activity ImageView imageView = findViewById(R.id.imageView); // Download directly from StorageReference using Glide // (See MyAppGlideModule for Loader registration) Glide.with(context) .load(storageReference) .into(imageView);
مدیریت تغییرات چرخه حیات فعالیت
دانلودها در پسزمینه ادامه مییابند حتی پس از تغییر چرخه عمر فعالیت (مانند ارائه یک گفتگو یا چرخاندن صفحه). هر شنونده ای که پیوست کرده بودید نیز پیوست خواهد ماند. اگر پس از توقف فعالیت با آنها تماس گرفته شود، این می تواند نتایج غیرمنتظره ای ایجاد کند.
می توانید این مشکل را با اشتراک شنوندگان خود با یک محدوده فعالیت حل کنید تا پس از توقف فعالیت، به طور خودکار آنها را لغو ثبت کنید. سپس، هنگام شروع مجدد فعالیت، از روش getActiveDownloadTasks
استفاده کنید تا کارهای دانلودی را که هنوز در حال اجرا هستند یا اخیراً تکمیل شده اند، بدست آورید.
مثال زیر این را نشان میدهد و همچنین نشان میدهد که چگونه میتوان مسیر مرجع ذخیرهسازی مورد استفاده را ادامه داد.
override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) // If there's a download in progress, save the reference so you can query it later outState.putString("reference", storageRef.toString()) } override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) // If there was a download in progress, get its reference and create a new StorageReference val stringRef = savedInstanceState.getString("reference") ?: return storageRef = Firebase.storage.getReferenceFromUrl(stringRef) // Find all DownloadTasks under this StorageReference (in this example, there should be one) val tasks = storageRef.activeDownloadTasks if (tasks.size > 0) { // Get the task monitoring the download val task = tasks[0] // Add new listeners to the task using an Activity scope task.addOnSuccessListener(this) { // Success! // ... } } }
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // If there's a download in progress, save the reference so you can query it later if (mStorageRef != null) { outState.putString("reference", mStorageRef.toString()); } } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // If there was a download in progress, get its reference and create a new StorageReference final String stringRef = savedInstanceState.getString("reference"); if (stringRef == null) { return; } mStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl(stringRef); // Find all DownloadTasks under this StorageReference (in this example, there should be one) List<FileDownloadTask> tasks = mStorageRef.getActiveDownloadTasks(); if (tasks.size() > 0) { // Get the task monitoring the download FileDownloadTask task = tasks.get(0); // Add new listeners to the task using an Activity scope task.addOnSuccessListener(this, new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { @Override public void onSuccess(FileDownloadTask.TaskSnapshot state) { // Success! // ... } }); } }
رسیدگی به خطاها
دلایل متعددی وجود دارد که ممکن است هنگام دانلود خطا رخ دهد، از جمله اینکه فایل موجود نیست یا کاربر اجازه دسترسی به فایل مورد نظر را ندارد. اطلاعات بیشتر در مورد خطاها را می توانید در بخش Handle Errors در اسناد پیدا کنید.
مثال کامل
یک مثال کامل از دانلود با مدیریت خطا در زیر نشان داده شده است:
storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener { // Use the bytes to display the image }.addOnFailureListener { // Handle any errors }
storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() { @Override public void onSuccess(byte[] bytes) { // Use the bytes to display the image } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
همچنین میتوانید فرادادههایی را برای فایلهایی که در Cloud Storage ذخیره میشوند دریافت و بهروزرسانی کنید .
،Cloud Storage for Firebase به شما این امکان را می دهد که به سرعت و به راحتی فایل ها را از یک سطل Cloud Storage که توسط Firebase تهیه و مدیریت می شود دانلود کنید.
یک مرجع ایجاد کنید
gs://
یا https://
موجود ایجاد کنید که به یک شی در Cloud Storage ارجاع میدهد.
// Create a storage reference from our app val storageRef = storage.reference // Create a reference with an initial file path and name val pathReference = storageRef.child("images/stars.jpg") // Create a reference to a file from a Google Cloud Storage URI val gsReference = storage.getReferenceFromUrl("gs://bucket/images/stars.jpg") // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! val httpsReference = storage.getReferenceFromUrl( "https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg", )
// Create a storage reference from our app StorageReference storageRef = storage.getReference(); // Create a reference with an initial file path and name StorageReference pathReference = storageRef.child("images/stars.jpg"); // Create a reference to a file from a Google Cloud Storage URI StorageReference gsReference = storage.getReferenceFromUrl("gs://bucket/images/stars.jpg"); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! StorageReference httpsReference = storage.getReferenceFromUrl("https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");
دانلود فایل ها
getBytes()
یا getStream()
فایل ها را از Cloud Storage دانلود کنید. اگر ترجیح می دهید فایل را با کتابخانه دیگری دانلود کنید، می توانید یک URL دانلود با getDownloadUrl()
دریافت کنید.
دانلود در حافظه
getBytes()
فایل را در byte[]
دانلود کنید. این ساده ترین راه برای دانلود یک فایل است، اما باید تمام محتویات فایل شما را در حافظه بارگیری کند. اگر فایلی بزرگتر از حافظه موجود برنامه خود درخواست کنید، برنامه شما خراب می شود. برای محافظت در برابر مشکلات حافظه، getBytes()
حداکثر مقدار بایت را برای دانلود نیاز دارد. حداکثر اندازه را روی چیزی تنظیم کنید که میدانید برنامهتان از پس آن بر میآید، یا از روش دانلود دیگری استفاده کنید.
var islandRef = storageRef.child("images/island.jpg") val ONE_MEGABYTE: Long = 1024 * 1024 islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener { // Data for "images/island.jpg" is returned, use this as needed }.addOnFailureListener { // Handle any errors }
StorageReference islandRef = storageRef.child("images/island.jpg"); final long ONE_MEGABYTE = 1024 * 1024; islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() { @Override public void onSuccess(byte[] bytes) { // Data for "images/island.jpg" is returns, use this as needed } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
دانلود به یک فایل محلی
getFile()
یک فایل را مستقیماً در یک دستگاه محلی دانلود می کند. اگر کاربران شما میخواهند در حالت آفلاین به فایل دسترسی داشته باشند یا فایل را در برنامه دیگری به اشتراک بگذارند، از این استفاده کنید. getFile()
یک DownloadTask
برمی گرداند که می توانید از آن برای مدیریت دانلود و نظارت بر وضعیت دانلود استفاده کنید.
islandRef = storageRef.child("images/island.jpg") val localFile = File.createTempFile("images", "jpg") islandRef.getFile(localFile).addOnSuccessListener { // Local temp file has been created }.addOnFailureListener { // Handle any errors }
islandRef = storageRef.child("images/island.jpg"); File localFile = File.createTempFile("images", "jpg"); islandRef.getFile(localFile).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 } });
دانلود داده ها از طریق URL
getDownloadUrl()
در مرجع Cloud Storage دریافت کنید.
storageRef.child("users/me/profile.png").downloadUrl.addOnSuccessListener { // Got the download URL for 'users/me/profile.png' }.addOnFailureListener { // Handle any errors }
storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { // Got the download URL for 'users/me/profile.png' } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
دانلود تصاویر با FirebaseUI
app/build.gradle
خود اضافه کنید:
dependencies { // FirebaseUI Storage only implementation 'com.firebaseui:firebase-ui-storage:7.2.0' }
سپس می توانید تصاویر را مستقیماً از Cloud Storage در ImageView
بارگیری کنید:
// Reference to an image file in Cloud Storage val storageReference = Firebase.storage.reference // ImageView in your Activity val imageView = findViewById<ImageView>(R.id.imageView) // Download directly from StorageReference using Glide // (See MyAppGlideModule for Loader registration) Glide.with(context) .load(storageReference) .into(imageView)
// Reference to an image file in Cloud Storage StorageReference storageReference = FirebaseStorage.getInstance().getReference(); // ImageView in your Activity ImageView imageView = findViewById(R.id.imageView); // Download directly from StorageReference using Glide // (See MyAppGlideModule for Loader registration) Glide.with(context) .load(storageReference) .into(imageView);
مدیریت تغییرات چرخه حیات فعالیت
دانلودها در پسزمینه ادامه مییابند حتی پس از تغییر چرخه عمر فعالیت (مانند ارائه یک گفتگو یا چرخاندن صفحه). هر شنونده ای که پیوست کرده بودید نیز پیوست خواهد ماند. اگر پس از توقف فعالیت با آنها تماس گرفته شود، این می تواند نتایج غیرمنتظره ای ایجاد کند.
می توانید این مشکل را با اشتراک شنوندگان خود با یک محدوده فعالیت حل کنید تا پس از توقف فعالیت، به طور خودکار آنها را لغو ثبت کنید. سپس، هنگام شروع مجدد فعالیت، از روش getActiveDownloadTasks
استفاده کنید تا کارهای دانلودی را که هنوز در حال اجرا هستند یا اخیراً تکمیل شده اند، بدست آورید.
مثال زیر این را نشان میدهد و همچنین نشان میدهد که چگونه میتوان مسیر مرجع ذخیرهسازی مورد استفاده را ادامه داد.
override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) // If there's a download in progress, save the reference so you can query it later outState.putString("reference", storageRef.toString()) } override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) // If there was a download in progress, get its reference and create a new StorageReference val stringRef = savedInstanceState.getString("reference") ?: return storageRef = Firebase.storage.getReferenceFromUrl(stringRef) // Find all DownloadTasks under this StorageReference (in this example, there should be one) val tasks = storageRef.activeDownloadTasks if (tasks.size > 0) { // Get the task monitoring the download val task = tasks[0] // Add new listeners to the task using an Activity scope task.addOnSuccessListener(this) { // Success! // ... } } }
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // If there's a download in progress, save the reference so you can query it later if (mStorageRef != null) { outState.putString("reference", mStorageRef.toString()); } } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // If there was a download in progress, get its reference and create a new StorageReference final String stringRef = savedInstanceState.getString("reference"); if (stringRef == null) { return; } mStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl(stringRef); // Find all DownloadTasks under this StorageReference (in this example, there should be one) List<FileDownloadTask> tasks = mStorageRef.getActiveDownloadTasks(); if (tasks.size() > 0) { // Get the task monitoring the download FileDownloadTask task = tasks.get(0); // Add new listeners to the task using an Activity scope task.addOnSuccessListener(this, new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { @Override public void onSuccess(FileDownloadTask.TaskSnapshot state) { // Success! // ... } }); } }
رسیدگی به خطاها
دلایل متعددی وجود دارد که ممکن است هنگام دانلود خطا رخ دهد، از جمله اینکه فایل موجود نیست یا کاربر اجازه دسترسی به فایل مورد نظر را ندارد. اطلاعات بیشتر در مورد خطاها را می توانید در بخش Handle Errors در اسناد پیدا کنید.
مثال کامل
یک مثال کامل از دانلود با مدیریت خطا در زیر نشان داده شده است:
storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener { // Use the bytes to display the image }.addOnFailureListener { // Handle any errors }
storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() { @Override public void onSuccess(byte[] bytes) { // Use the bytes to display the image } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } });
همچنین میتوانید فرادادههایی را برای فایلهایی که در Cloud Storage ذخیره میشوند دریافت و بهروزرسانی کنید .