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

Android में ड्रॉएबल (Drawable) को एक्टिविटी के बीच कैसे पास करें?

Android में Intent के माध्यम से ड्रॉएबल संसाधन पास करने का गाइड

यह ट्यूटोरियल दिखाता है कि कैसे ड्रॉएबल रिसोर्स ID (इंटीजर) को Intent extra के रूप में दूसरी एक्टिविटी में पास किया जाए और वहां ImageView में सेट किया जाए।

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

Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।

चरण 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">
    <Button
        android:id="@+id/button"
        android:text="Transfer Bitmap to 2nd Activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>

चरण 3: MainActivity.java में कोड जोड़ें

src/MainActivity.java में:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("picture", R.drawable.ic_directions_walk);
                startActivity(intent);
            }
        });
    }
}

चरण 4: दूसरी एक्टिविटी (SecondActivity.java)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class SecondActivity extends AppCompatActivity {
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        imageView = findViewById(R.id.imageView);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            int picture = bundle.getInt("picture");
            imageView.setImageResource(picture);
        }
    }
}

चरण 5: दूसरी एक्टिविटी का लेआउट (activity_second.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=".SecondActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Activity"
        android:layout_marginTop="25sp"
        android:layout_centerHorizontal="true"
        android:textSize="16sp"
        android:textStyle="bold"/>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
</RelativeLayout>

चरण 6: AndroidManifest.xml में दोनों एक्टिविटी घोषित करें

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

महत्वपूर्ण नोट्स

  • ड्रॉएबल ऑब्जेक्ट सीधे पास नहीं किया जा सकता (Parcelable नहीं है)
  • रिसोर्स ID (R.drawable.xxx) इंटीजर होता है, आसानी से पास होता है
  • बिटमैप पास करने के लिए Bitmap.compress() से बाइट ऐरे या फाइल URI उपयोग करें
  • बड़े बिटमैप के लिए Intent extra उपयुक्त नहीं - फाइल/कंटेंट प्रोवाइडर उपयोग करें

ऐप चलाएं

डिवाइस कनेक्ट करें, Run आइकन पर क्लिक करें, बटन दबाएं और दूसरी एक्टिविटी में इमेज देखें।

  1. Android में एक्टिविटी के बीच स्विच कैसे करें? Android में एक्टिविटी के बीच स्विच कैसे करें?

    एक्टिविटी के बीच नेविगेशन यह उदाहरण दर्शाता है कि Intent का उपयोग करके दो एक्टिविटी के बीच आगे-पीछे कैसे नेविगेट करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मुख्य लेआउट (activity_main.xml) <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:an

  1. एंड्रॉइड में Bitmap को Drawable में कैसे कन्वर्ट करें? एंड्रॉइड में Bitmap को Drawable में कैसे कन्वर्ट करें?

    एंड्रॉइड में Bitmap और Drawable के बीच कन्वर्जन यह उदाहरण दिखाता है कि `BitmapDrawable` क्लास का उपयोग करके Bitmap को Drawable में और Drawable को Bitmap में कैसे बदला जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML res/layout/ac

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

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