Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में Glide लाइब्रेरी से इमेज को Bitmap में कैसे डाउनलोड करें?

परिचय

Glide (बम्पटेक द्वारा) एक शक्तिशाली इमेज लोडिंग लाइब्रेरी है जो इमेज को Bitmap ऑब्जेक्ट के रूप में डाउनलोड करने की अनुमति देती है। यह तब उपयोगी है जब आपको इमेज पर प्रोसेसिंग करनी हो या उसे ImageView के बाहर उपयोग करना हो।

आवश्यक चरण

चरण 1: नया प्रोजेक्ट बनाएं और डिपेंडेंसी जोड़ें

Android Studio में File → New Project से नया प्रोजेक्ट बनाएं। build.gradle (Module: app) में:

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.16.0'  // नवीनतम वर्जन
    annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'  // GeneratedAPI के लिए
}

चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

</RelativeLayout>

चरण 3: मुख्य गतिविधि (src/MainActivity.java)

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);

        // Bitmap के रूप में लोड करें
        Glide.with(this)
            .asBitmap()
            .load("https://www.google.es/images/srpr/logo11w.png")
            .into(new CustomTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    // Bitmap तैयार है - यहां प्रोसेसिंग करें या ImageView में सेट करें
                    imageView.setImageBitmap(resource);

                    // उदाहरण: Bitmap को फाइल में सेव करें
                    // saveBitmapToFile(resource);
                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {
                    // रिसोर्स क्लियर होने पर (जैसे कॉन्फिग चेंज)
                }
            });
    }
}

चरण 4: मैनिफेस्ट फ़ाइल (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

ऐप चलाएं

डिवाइस कनेक्ट करें, Run क्लिक करें। Google लोगो Bitmap के रूप में डाउनलोड होकर ImageView में दिखेगा।

Android में Glide लाइब्रेरी से इमेज को Bitmap में कैसे डाउनलोड करें?

Glide में Bitmap लोडिंग के उन्नत विकल्प

1. साइज स्पेसिफाई करें (मेमोरी बचाने के लिए)

Glide.with(this)
    .asBitmap()
    .load(url)
    .apply(new RequestOptions().override(800, 600))  // मैक्स डायमेंशन
    .into(customTarget);

2. Bitmap को फाइल में सेव करें

private void saveBitmapToFile(Bitmap bitmap) {
    Executors.newSingleThreadExecutor().execute(() -> {
        File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "downloaded_image.png");
        try (FileOutputStream fos = new FileOutputStream(file)) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            runOnUiThread(() -> Toast.makeText(this, "सेव: " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show());
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
}

3. सिंपल टार्गेट (डिप्रिकेटेड लेकिन सरल)

Glide.with(this)
    .asBitmap()
    .load(url)
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
            imageView.setImageBitmap(resource);
        }
    });

4. प्रीलोडिंग (बैकग्राउंड में कैश करें)

Glide.with(this)
    .asBitmap()
    .load(url)
    .preload(); // बाद में जल्दी लोड होगा

महत्वपूर्ण बिंदु

  • .asBitmap() - Glide को बताता है कि Bitmap ऑब्जेक्ट चाहिए, Drawable नहीं।
  • CustomTarget - कस्टम साइज और कॉलबैक के लिए। SimpleTarget डिप्रिकेटेड है।
  • हमेशा onLoadCleared में क्लीनअप करें यदि प्लेसहोल्डर ड्रॉएबल उपयोग कर रहे हैं।
  • बड़े Bitmap मेमोरी खाते हैं - override() से डाउनसैंपल करें।
  • Glide लाइफसाइकिल-अवेयर है - एक्टिविटी/फ्रैगमेंट डिस्ट्रॉय पर ऑटो कैंसल करता है।
  1. Android में दी गई URL से इमेज डाउनलोड करके कैसे सेव करें? Android में दी गई URL से इमेज डाउनलोड करके कैसे सेव करें?

    Android में AsyncTask से URL से इमेज डाउनलोड करने का गाइड यह ट्यूटोरियल दिखाता है कि कैसे AsyncTask, HttpURLConnection और BitmapFactory का उपयोग करके बैकग्राउंड थ्रेड में इमेज डाउनलोड करके ImageView में दिखाया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट

  1. Android में Drawable को Bitmap में कैसे बदलें? Android में Drawable को Bitmap में कैसे बदलें?

    Android में Drawable से Bitmap कन्वर्जन का तरीका यह उदाहरण दिखाता है कि ड्रॉएबल रिसोर्स (जैसे @drawable/image) को Bitmap ऑब्जेक्ट में कैसे बदला जाता है ताकि इमेज प्रोसेसिंग या मैनिपुलेशन की जा सके। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट फ़ाइल (res/layo

  1. Android में Glide लाइब्रेरी से इमेज को Bitmap में कैसे डाउनलोड करें? Android में Glide लाइब्रेरी से इमेज को Bitmap में कैसे डाउनलोड करें?

    परिचय Glide (बम्पटेक द्वारा) एक शक्तिशाली इमेज लोडिंग लाइब्रेरी है जो इमेज को Bitmap ऑब्जेक्ट के रूप में डाउनलोड करने की अनुमति देती है। यह तब उपयोगी है जब आपको इमेज पर प्रोसेसिंग करनी हो या उसे ImageView के बाहर उपयोग करना हो। आवश्यक चरण चरण 1: नया प्रोजेक्ट बनाएं और डिपेंडेंसी जोड़ें Android Stu