'Firebase के लिए Cloud Storage' की मदद से, Firebase की ओर से उपलब्ध कराए गए और मैनेज किए जा रहे Cloud Storage बकेट से फ़ाइलों को तेज़ी और आसानी से डाउनलोड किया जा सकता है.
रेफ़रंस बनाना
किसी फ़ाइल को डाउनलोड करने के लिए, पहले उस फ़ाइल के लिए Cloud Storage रेफ़रंस बनाएं जिसे आपको डाउनलोड करना है.
अपने Cloud Storage बकेट के रूट में चाइल्ड पाथ जोड़कर, रेफ़रंस बनाया जा सकता है. इसके अलावा, Cloud Storage में मौजूद किसी ऑब्जेक्ट के बारे में बताने वाले मौजूदा gs://
या https://
यूआरएल से भी रेफ़रंस बनाया जा सकता है.
Kotlin+KTX
// 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", )
Java
// 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 से फ़ाइलें डाउनलोड कर सकते हैं. अगर आपको किसी दूसरी लाइब्रेरी से फ़ाइल डाउनलोड करनी है, तो आपको getDownloadUrl()
की मदद से, डाउनलोड करने का यूआरएल मिल सकता है.
मेमोरी में डाउनलोड करें
getBytes()
तरीके का इस्तेमाल करके फ़ाइल को byte[]
में डाउनलोड करें. यह किसी फ़ाइल को डाउनलोड करने का सबसे आसान तरीका है. हालांकि, इसके लिए आपकी फ़ाइल का पूरा कॉन्टेंट, मेमोरी में लोड होना चाहिए. अगर आप अपने ऐप्लिकेशन के उपलब्ध मेमोरी से बड़ी फ़ाइल का अनुरोध
करते हैं, तो आपका ऐप्लिकेशन क्रैश हो जाएगा. मेमोरी की समस्याओं से सुरक्षा के लिए, getBytes()
डाउनलोड करने में ज़्यादा से ज़्यादा बाइट लेता है. किसी ऐसी चीज़ के लिए ज़्यादा से ज़्यादा साइज़ सेट करें
जिसे आपके ऐप्लिकेशन में मैनेज किया जा सकता है. इसके अलावा, डाउनलोड करने के किसी दूसरे तरीके का इस्तेमाल भी किया जा सकता है.
Kotlin+KTX
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 }
Java
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
दिखाता है. इसका इस्तेमाल करके, डाउनलोड को मैनेज किया जा सकता है और डाउनलोड की स्थिति को मॉनिटर किया जा सकता है.
Kotlin+KTX
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 }
Java
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 } });
अगर आपको डाउनलोड किए गए कॉन्टेंट को सही तरीके से मैनेज करना है, तो ज़्यादा जानकारी के लिए, डाउनलोड मैनेज करना लेख पढ़ें.
यूआरएल की मदद से डेटा डाउनलोड करना
अगर आपके पास पहले से ही यूआरएल से जुड़ा डाउनलोड इन्फ़्रास्ट्रक्चर है या आपको सिर्फ़ यूआरएल शेयर करना है, तो किसी फ़ाइल के डाउनलोड का यूआरएल पाने के लिए, Cloud Storage रेफ़रंस पर getDownloadUrl()
तरीके का इस्तेमाल करें.
Kotlin+KTX
storageRef.child("users/me/profile.png").downloadUrl.addOnSuccessListener { // Got the download URL for 'users/me/profile.png' }.addOnFailureListener { // Handle any errors }
Java
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 की मदद से इमेज डाउनलोड की जा रही हैं
FirebaseUI, बॉयलरप्लेट कोड को हटाने और Google के सबसे सही तरीकों को बढ़ावा देने के लिए आसान, कस्टमाइज़ की जा सकने वाली, और प्रोडक्शन के लिए तैयार नेटिव मोबाइल बाइंडिंग उपलब्ध कराता है. FirebaseUI का इस्तेमाल करके Glide के साथ हमारे इंटिग्रेशन का इस्तेमाल करके, Cloud Storage से इमेज को तेज़ी और आसानी से डाउनलोड, कैश, और दिखाया जा सकता है.
सबसे पहले, अपने app/build.gradle
में FirebaseUI जोड़ें:
dependencies { // FirebaseUI Storage only implementation 'com.firebaseui:firebase-ui-storage:7.2.0' }
इसके बाद, सीधे Cloud Storage से इमेज को ImageView
में लोड किया जा सकता है:
Kotlin+KTX
// 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)
Java
// 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
तरीके का इस्तेमाल करके, ऐसे डाउनलोड टास्क पाएं जो अब भी चल रहे हैं या हाल ही में पूरे हुए हैं.
नीचे दिया गया उदाहरण इसे दिखाता है. साथ ही, यह भी दिखाता है कि स्टोरेज के लिए इस्तेमाल किए गए रेफ़रंस पाथ को कैसे बनाए रखें.
Kotlin+KTX
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! // ... } } }
Java
@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! // ... } }); } }
गड़बड़ियां ठीक करना
डाउनलोड करते समय गड़बड़ियां होने की कई वजहें हो सकती हैं. इनमें, फ़ाइल का मौजूद न होना या उपयोगकर्ता को अपनी पसंद की फ़ाइल ऐक्सेस करने की अनुमति न होना शामिल है. गड़बड़ियों के बारे में ज़्यादा जानकारी, दस्तावेज़ों के गड़बड़ियां मैनेज करना सेक्शन में मिल सकती है.
पूरा उदाहरण
गड़बड़ी ठीक करने के तरीके के साथ डाउनलोड का पूरा उदाहरण नीचे दिखाया गया है:
Kotlin+KTX
storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener { // Use the bytes to display the image }.addOnFailureListener { // Handle any errors }
Java
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 में सेव की गई फ़ाइलों के लिए, मेटाडेटा को डाउनलोड और अपडेट किया जा सकता है.