बड़े बिटमैप को बेहतर तरीके से लोड करना

ध्यान दें: यूज़र इंटरफ़ेस (यूआई) की इमेज लोड करने के लिए, मैन्युअल तरीके से बिट मैप डिकोड करने के बजाय, इमेज लोड करने वाली लाइब्रेरी का इस्तेमाल करें. जैसे, Coil या Glide. बिट मैप मेमोरी आर्किटेक्चर, अडैप्टिव कैश साइज़िंग, और लाइफ़साइकल इविक्शन के बारे में पूरी जानकारी पाने के लिए, बिट मैप मेमोरी को ऑप्टिमाइज़ करना लेख पढ़ें.

इमेज कई तरह की और अलग-अलग साइज़ की होती हैं. कई मामलों में, ये ऐप्लिकेशन के सामान्य यूज़र इंटरफ़ेस (यूआई) के लिए ज़रूरी साइज़ से बड़े होते हैं. उदाहरण के लिए, सिस्टम का Gallery ऐप्लिकेशन, आपके Android डिवाइस के कैमरे से ली गई फ़ोटो दिखाता है. आम तौर पर, इन फ़ोटो का रिज़ॉल्यूशन आपके डिवाइस की स्क्रीन डेंसिटी से ज़्यादा होता है.

आपके पास सीमित मेमोरी है. इसलिए, आपको मेमोरी में सिर्फ़ कम रिज़ॉल्यूशन वाला वर्शन लोड करना चाहिए. कम रिज़ॉल्यूशन वाले वर्शन का साइज़, उसे दिखाने वाले यूज़र इंटरफ़ेस (यूआई) कॉम्पोनेंट के साइज़ से मेल खाना चाहिए. ज़्यादा रिज़ॉल्यूशन वाली इमेज से कोई खास फ़ायदा नहीं होता. हालांकि, इससे मेमोरी का ज़्यादा इस्तेमाल होता है. साथ ही, इमेज को तुरंत स्केल करने की वजह से, परफ़ॉर्मेंस पर भी असर पड़ता है.

इस गाइड में, स्केल किए गए बिटमैप को मैन्युअल तरीके से डिकोड करने का तरीका बताया गया है. यह Android 9 (एपीआई लेवल 28) और इसके बाद के वर्शन पर ImageDecoder, ज़ूम की जा सकने वाली बड़ी इमेज के लिए BitmapRegionDecoder, और Android 8.1 (एपीआई लेवल 27) और इससे पहले के वर्शन पर BitmapFactory को कवर करता है.

ImageDecoder की मदद से, स्केल किए गए हार्डवेयर बिटमैप को डिकोड करना

Android 9 (एपीआई लेवल 28) और इसके बाद के वर्शन पर, दो-पास BitmapFactory डिकोडिंग के बजाय ImageDecoder का इस्तेमाल करें. एन्कोड की गई इमेज का साइज़ पढ़ने और आउटपुट को टारगेट की सीमाओं के अंदर स्केल करने के लिए, OnHeaderDecodedListener पास करें. ALLOCATOR_HARDWARE को सेट करें, ताकि ग्राफ़िक्स मेमोरी में ऐसे यूज़र इंटरफ़ेस (यूआई) बिटमैप रखे जा सकें जिनमें बदलाव न किया जा सकता हो. हार्डवेयर बिट मैप को अब भी Android की ज़रूरी जानकारी वाली बिट मैप मेमोरी मेट्रिक में गिना जाता है. इसलिए, उन्हें कंटेनर की सीमाओं के हिसाब से साइज़ दें:

val source = ImageDecoder.createSource(resources, R.drawable.myimage)
val bitmap = ImageDecoder.decodeBitmap(source) { decoder, info, _ ->
    decoder.allocator = ImageDecoder.ALLOCATOR_HARDWARE
    val scale = minOf(
        reqWidth.toFloat() / info.size.width,
        reqHeight.toFloat() / info.size.height,
        1f,
    )
    if (scale < 1f) {
        decoder.setTargetSize(
            (info.size.width * scale).roundToInt().coerceAtLeast(1),
            (info.size.height * scale).roundToInt().coerceAtLeast(1),
        )
    }
}

BitmapRegionDecoder की मदद से, दिखने वाली टाइल को डिकोड करना

मैप, फ़्लोर प्लान या प्रॉडक्ट की झलक जैसी बड़ी और ज़ूम की जा सकने वाली इमेज के लिए, BitmapRegionDecoder का इस्तेमाल करें. decodeRegion को कॉल करें (ज़ूम आउट करने पर BitmapFactory.Options.inSampleSize के साथ), ताकि रैम में मौजूद फ़ुल रिज़ॉल्यूशन वाली इमेज के बजाय, सिर्फ़ दिखने वाले व्यूपोर्ट Rect को डिकोड किया जा सके.

बिटमैप के डाइमेंशन और टाइप को पढ़ना (BitmapFactory)

BitmapFactory क्लास, अलग-अलग सोर्स से Bitmap बनाने के लिए, डिकोड करने के कई तरीके (decodeByteArray(), decodeFile(), decodeResource() वगैरह) उपलब्ध कराती है. अपने इमेज डेटा सोर्स के आधार पर, डिकोड करने का सबसे सही तरीका चुनें. ये तरीके, बनाए गए बिट मैप के लिए मेमोरी असाइन करने की कोशिश करते हैं. इसलिए, इनसे आसानी से OutOfMemory अपवाद हो सकता है. हर तरह के डिकोड तरीके में अतिरिक्त सिग्नेचर होते हैं. इनकी मदद से, BitmapFactory.Options क्लास का इस्तेमाल करके डिकोड करने के विकल्प तय किए जा सकते हैं. डिकोड करते समय inJustDecodeBounds प्रॉपर्टी को true पर सेट करने से, मेमोरी का बंटवारा नहीं होता. इससे बिटमैप ऑब्जेक्ट के लिए null मिलता है, लेकिन outWidth, outHeight, और outMimeType सेट हो जाता है. इस तकनीक की मदद से, बिट मैप बनाने (और मेमोरी का बंटवारा करने) से पहले, इमेज डेटा के डाइमेंशन और टाइप को पढ़ा जा सकता है.

Kotlin

val options = BitmapFactory.Options().apply {
    inJustDecodeBounds = true
}
BitmapFactory.decodeResource(resources, R.id.myimage, options)
val imageHeight: Int = options.outHeight
val imageWidth: Int = options.outWidth
val imageType: String = options.outMimeType

Java

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

java.lang.OutOfMemory अपवादों से बचने के लिए, बिट मैप को डिकोड करने से पहले उसके डाइमेंशन की जांच करें. हालांकि, अगर आपको सोर्स पर पूरा भरोसा है कि वह आपको अनुमानित साइज़ का इमेज डेटा उपलब्ध कराएगा, तो ऐसा करने की ज़रूरत नहीं है. यह डेटा, उपलब्ध मेमोरी में आसानी से फ़िट हो जाता है.

मेमोरी में छोटा वर्शन लोड करना

इमेज के डाइमेंशन की जानकारी मिलने के बाद, यह तय किया जा सकता है कि पूरी इमेज को मेमोरी में लोड किया जाए या सबसैंपल किया गया वर्शन लोड किया जाए. इन बातों का ध्यान रखें:

  • मेमोरी में पूरी इमेज लोड करने के लिए, मेमोरी के इस्तेमाल का अनुमान.
  • इस इमेज को लोड करने के लिए, आपको कितनी मेमोरी की ज़रूरत है. यह आपके ऐप्लिकेशन की मेमोरी से जुड़ी अन्य ज़रूरी शर्तों पर निर्भर करता है.
  • टारगेट ImageView या यूज़र इंटरफ़ेस (यूआई) कॉम्पोनेंट के डाइमेंशन, जिसमें इमेज लोड की जानी है.
  • मौजूदा डिवाइस की स्क्रीन का साइज़ और डेंसिटी.

उदाहरण के लिए, अगर किसी इमेज को ImageView में 128x96 पिक्सल के थंबनेल में दिखाया जाना है, तो उसे 1024x768 पिक्सल की इमेज के तौर पर मेमोरी में लोड करने का कोई फ़ायदा नहीं है.

इमेज को छोटे बिटमैप में सबसैंपल करने के लिए, अपने BitmapFactory.Options ऑब्जेक्ट में inSampleSize को दो के पावर वाले सैंपल साइज़ पर सेट करें. उदाहरण के लिए, 2048x1536 रिज़ॉल्यूशन वाली इमेज को 4 के inSampleSize के साथ डिकोड करने पर, करीब 512x384 का बिट मैप बनता है. इसे मेमोरी में लोड करने पर, पूरी इमेज के लिए 12 एमबी के बजाय 0.75 एमबी का इस्तेमाल होता है. ऐसा तब होता है, जब ARGB_8888 का बिटमैप कॉन्फ़िगरेशन हो. यहां सैंपल साइज़ की वैल्यू का हिसाब लगाने का तरीका बताया गया है. यह वैल्यू, टारगेट की गई चौड़ाई और ऊंचाई के आधार पर दो की घात होती है:

Kotlin

fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
    // Raw height and width of image
    val (height: Int, width: Int) = options.run { outHeight to outWidth }
    var inSampleSize = 1

    if (height > reqHeight || width > reqWidth) {

        val halfHeight: Int = height / 2
        val halfWidth: Int = width / 2

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
            inSampleSize *= 2
        }
    }

    return inSampleSize
}

Java

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

ध्यान दें: दो की घात वाली वैल्यू का हिसाब लगाया जाता है, क्योंकि डिकोडर inSampleSize दस्तावेज़ के मुताबिक, दो की घात वाली सबसे नज़दीकी वैल्यू को राउंड डाउन करके फ़ाइनल वैल्यू का इस्तेमाल करता है.

इस तरीके का इस्तेमाल करने के लिए, पहले inJustDecodeBounds को true पर सेट करके डिकोड करें. इसके बाद, विकल्पों को पास करें और फिर नई inSampleSize वैल्यू और inJustDecodeBounds को false पर सेट करके फिर से डिकोड करें:

Kotlin

fun decodeSampledBitmapFromResource(
        res: Resources,
        resId: Int,
        reqWidth: Int,
        reqHeight: Int
): Bitmap {
    // First decode with inJustDecodeBounds=true to check dimensions
    return BitmapFactory.Options().run {
        inJustDecodeBounds = true
        BitmapFactory.decodeResource(res, resId, this)

        // Calculate inSampleSize
        inSampleSize = calculateInSampleSize(this, reqWidth, reqHeight)

        // Decode bitmap with inSampleSize set
        inJustDecodeBounds = false

        BitmapFactory.decodeResource(res, resId, this)
    }
}

Java

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

इस तरीके से, किसी भी बड़े साइज़ के बिटमैप को ImageView में आसानी से लोड किया जा सकता है. यह 100x100 पिक्सल का थंबनेल दिखाता है, जैसा कि यहां दिए गए उदाहरण कोड में दिखाया गया है:

Kotlin

imageView.setImageBitmap(
        decodeSampledBitmapFromResource(resources, R.id.myimage, 100, 100)
)

Java

imageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

अन्य सोर्स से बिटमैप को डिकोड करने के लिए, इसी तरह की प्रोसेस अपनाई जा सकती है. इसके लिए, ज़रूरत के हिसाब से सही BitmapFactory.decode* तरीके का इस्तेमाल करें.