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

Kotlin का उपयोग करके Android पर URL द्वारा ImageView कैसे लोड करें?

परिचय

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android पर URL द्वारा ImageView कैसे लोड किया जाए।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio खोलें, File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।

चरण 2: activity_main.xml लेआउट जोड़ें

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"
    android:padding="4dp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="Load Image From URL in Android ImageView"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="24sp"
        android:textStyle="bold" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/text"
        android:layout_marginTop="5dp" />
</RelativeLayout>

चरण 3: MainActivity.kt लागू करें

src/MainActivity.kt में निम्न कोड जोड़ें:

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        
        DownloadImageFromInternet(findViewById(R.id.imageView)).execute(
            "https://images.unsplash.com/photo-1535332371349-a5d229f49cb5?ixlib=rb-1.2.1&w=1000&q=80"
        )
    }
    
    @SuppressLint("StaticFieldLeak")
    @Suppress("DEPRECATION")
    private inner class DownloadImageFromInternet(var imageView: ImageView) : AsyncTask<String, Void, Bitmap?>() {
        
        init {
            Toast.makeText(applicationContext, "Please wait, it may take a few minute...", Toast.LENGTH_SHORT).show()
        }
        
        override fun doInBackground(vararg urls: String): Bitmap? {
            val imageURL = urls[0]
            var image: Bitmap? = null
            try {
                val `in` = java.net.URL(imageURL).openStream()
                image = BitmapFactory.decodeStream(`in`)
            } catch (e: Exception) {
                Log.e("Error Message", e.message.toString())
                e.printStackTrace()
            }
            return image
        }
        
        override fun onPostExecute(result: Bitmap?) {
            imageView.setImageBitmap(result)
        }
    }
}

चरण 4: AndroidManifest.xml कॉन्फ़िगर करें

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.q11">
    <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>

एप्लिकेशन चलाएं

अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफ़ॉल्ट स्क्रीन देखें।

सुझाव: आधुनिक Android विकास के लिए, AsyncTask डिप्रिकेटेड है। बेहतर प्रदर्शन और जीवनचक्र प्रबंधन के लिए Glide, Picasso, या Coil जैसी छवि लोडिंग लाइब्रेरी का उपयोग करने पर विचार करें।
  1. Android में कोड का उपयोग करके ImageView का मार्जिन कैसे सेट करें Android में कोड का उपयोग करके ImageView का मार्जिन कैसे सेट करें

    परिचयरनटाइम पर व्यू के मार्जिन को बदलने के लिए, उसके `LayoutParams` को प्राप्त करें, मार्जिन सेट करें, और फिर `setLayoutParams()` कॉल करें।कार्यान्वयनचरण 1: लेआउट (activity_main.xml)<ImageView android:id=@+id/imageView android:layout_width=wrap_content android:layout_height=wrap_content android:b

  1. एंड्रॉइड में URL से ImageView में इमेज कैसे लोड करें? एंड्रॉइड में URL से ImageView में इमेज कैसे लोड करें?

    एंड्रॉइड में इंटरनेट से इमेज लोड करने का आसान तरीका यह उदाहरण दर्शाता है कि AsyncTask का उपयोग करके URL से ImageView में इमेज कैसे लोड करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरें। चरण 2: लेआउट में ImageView जोड़ें res/layout/activity_main.xml

  1. एंड्रॉइड ऐप में ImageView में इमेज कैसे लोड और प्रदर्शित करें? एंड्रॉइड ऐप में ImageView में इमेज कैसे लोड और प्रदर्शित करें?

    बटन क्लिक पर ड्रॉएबल रिसोर्स से इमेज बदलें यह उदाहरण दर्शाता है कि ImageView में ड्रॉएबल रिसोर्स से इमेज कैसे लोड करें और बटन क्लिक पर बदलें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट में ImageView और बटन res/layout/activity_main.xml: <?xml version=1.0