उद्देश्य
यह उदाहरण दर्शाता है कि Android में प्रोग्रामेटिकली स्क्रीनशॉट कैसे लें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML जोड़ें
निम्न कोड को res/layout/activity_main.xml में जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#33FFFF00"
android:orientation="vertical">
<ImageView
android:id="@+id/screenShot"
android:layout_width="300dp"
android:layout_height="300dp" />
<TextView
android:id="@+id/text"
android:textSize="18sp"
android:layout_gravity="center"
android:text="Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ऊपर के कोड में हमने दो व्यू लिए हैं: ImageView और TextView। जब यूजर TextView पर क्लिक करता है, तो यह स्क्रीनशॉट लेकर ImageView में दिखाता है।
चरण 3: MainActivity जावा कोड जोड़ें
निम्न कोड को src/MainActivity.java में जोड़ें:
package com.example.andy.myapplication;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int view = R.layout.activity_main;
ImageView screenShot;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
final LinearLayout parent = findViewById(R.id.parent);
screenShot = findViewById(R.id.screenShot);
textView = findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("It is screen shot text");
screenShot(parent);
}
});
}
public void screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
screenShot.setImageBitmap(bitmap);
textView.setText("click");
}
}
स्क्रीनशॉट कोड समझाया गया
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); screenShot.setImageBitmap(bitmap);
ऊपर के कोड में हमने screenShot(view) मेथड लिखा है, जिसमें हमने पैरेंट व्यू को पास किया है ताकि टॉप से बॉटम तक स्क्रीनशॉट लिया जा सके।
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफ़ॉल्ट स्क्रीन देखें:

अब TextView पर क्लिक करें, यह स्क्रीनशॉट लेकर ImageView में दिखाएगा:

महत्वपूर्ण नोट्स
- यह मेथड केवल विजिबल व्यू का स्क्रीनशॉट लेता है, पूरे स्क्रीन का नहीं।
- Android 10+ में स्क्रीनशॉट लेने के लिए
MediaProjectionAPI का उपयोग करना पड़ता है। - इस तरीके से सिस्टम UI (स्टेटस बार, नेविगेशन बार) कैप्चर नहीं होता।