एंड्रॉइड पर क्लाउड स्टोरेज के साथ फ़ाइलें डाउनलोड करें

फायरबेस के लिए क्लाउड स्टोरेज आपको फायरबेस द्वारा प्रदान और प्रबंधित क्लाउड स्टोरेज बकेट से फ़ाइलों को जल्दी और आसानी से डाउनलोड करने की अनुमति देता है।

एक संदर्भ बनाएँ

किसी फ़ाइल को डाउनलोड करने के लिए, पहले उस फ़ाइल का क्लाउड स्टोरेज संदर्भ बनाएं जिसे आप डाउनलोड करना चाहते हैं।

आप अपने क्लाउड स्टोरेज बकेट के रूट में चाइल्ड पाथ जोड़कर एक संदर्भ बना सकते हैं, या आप क्लाउड स्टोरेज में किसी ऑब्जेक्ट को संदर्भित करने वाले मौजूदा 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() पर कॉल करके क्लाउड स्टोरेज से फ़ाइलें डाउनलोड कर सकते हैं। यदि आप फ़ाइल को किसी अन्य लाइब्रेरी से डाउनलोड करना पसंद करते हैं, तो आप 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
    }
});

यदि आप अपने डाउनलोड को सक्रिय रूप से प्रबंधित करना चाहते हैं, तो अधिक जानकारी के लिए डाउनलोड प्रबंधित करें देखें।

यूआरएल के माध्यम से डेटा डाउनलोड करें

यदि आपके पास पहले से ही यूआरएल पर आधारित डाउनलोड इंफ्रास्ट्रक्चर है, या आप साझा करने के लिए एक यूआरएल चाहते हैं, तो आप क्लाउड स्टोरेज संदर्भ पर 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 के साथ छवियाँ डाउनलोड करना

फायरबेसयूआई बॉयलरप्लेट कोड को खत्म करने और Google की सर्वोत्तम प्रथाओं को बढ़ावा देने के लिए सरल, अनुकूलन योग्य और उत्पादन-तैयार देशी मोबाइल बाइंडिंग प्रदान करता है। फायरबेसयूआई का उपयोग करके आप ग्लाइड के साथ हमारे एकीकरण का उपयोग करके क्लाउड स्टोरेज से छवियों को जल्दी और आसानी से डाउनलोड, कैश और प्रदर्शित कर सकते हैं।

सबसे पहले, अपने app/build.gradle में FirebaseUI जोड़ें:

dependencies {
    // FirebaseUI Storage only
    implementation 'com.firebaseui:firebase-ui-storage:7.2.0'
}

फिर आप छवियों को सीधे क्लाउड स्टोरेज से 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
    }
});

आप क्लाउड स्टोरेज में संग्रहीत फ़ाइलों के लिए मेटाडेटा भी प्राप्त और अपडेट कर सकते हैं।