Cloud Storage for Firebase से आपको तुरंत और आसानी से डाउनलोड करने की सुविधा मिलती है Cloud Storage की फ़ाइलें Firebase उपलब्ध कराने और मैनेज करने के लिए बकेट.
रेफ़रंस बनाना
फ़ाइल डाउनलोड करने के लिए, सबसे पहले 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");
फ़ाइलें डाउनलोड करें
पहचान फ़ाइल मिलने के बाद, आप Cloud Storage से फ़ाइलें डाउनलोड कर सकते हैं
getBytes()
या getStream()
पर कॉल करके. अगर आपको फ़ाइल डाउनलोड करनी है, तो
तो आपको 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 } });
अगर आपको डाउनलोड की गई फ़ाइलों को समय-समय पर मैनेज करना है, तो ज़्यादा जानकारी के लिए, डाउनलोड मैनेज करें.
यूआरएल की मदद से डेटा डाउनलोड करना
अगर आपके पास पहले से ही यूआरएल के हिसाब से डाउनलोड इन्फ़्रास्ट्रक्चर है या आपको
कोई URL है, तो आप फ़ाइल के डाउनलोड URL को
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 का इस्तेमाल करके, ये काम किए जा सकते हैं तेज़ी और आसानी से डाउनलोड, कैश, और डिसप्ले इमेज से हमारे एकीकरण का उपयोग करके 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 में सेव की गई फ़ाइलों के लिए.