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

Kotlin का उपयोग करके Android में प्रोग्रामेटिक रूप से स्क्रीनशॉट कैसे लें?

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android में वर्तमान स्क्रीन का स्क्रीनशॉट प्रोग्रामेटिक रूप से कैसे लिया जाए।

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

Android Studio में File → New Project।

चरण 2 - activity_main.xml लेआउट

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btnScreenshot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="स्क्रीनशॉट लें" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/btnScreenshot"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:scaleType="fitCenter" />
</RelativeLayout>

चरण 3 - MainActivity.kt में स्क्रीनशॉट लॉजिक

import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

@Suppress("DEPRECATION")
class MainActivity : AppCompatActivity() {
    private lateinit var rootView: View
    private lateinit var imageView: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        rootView = findViewById(R.id.mainLayout)
        imageView = findViewById(R.id.imageView)
        val btnScreenshot = findViewById<Button>(R.id.btnScreenshot)

        btnScreenshot.setOnClickListener {
            val bitmap = takeScreenshotOfRootView(rootView)
            imageView.setImageBitmap(bitmap)
            rootView.setBackgroundColor(Color.parseColor("#999999"))
        }
    }

    companion object {
        private fun takeScreenshot(view: View): Bitmap {
            view.isDrawingCacheEnabled = true
            view.buildDrawingCache(true)
            val bitmap = Bitmap.createBitmap(view.drawingCache)
            view.isDrawingCacheEnabled = false
            return bitmap
        }

        fun takeScreenshotOfRootView(view: View): Bitmap {
            return takeScreenshot(view.rootView)
        }
    }
}

चरण 4 - AndroidManifest.xml

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

ऐप चलाएं

ऐप चलाएं, "स्क्रीनशॉट लें" बटन दबाएं। वर्तमान स्क्रीन का स्क्रीनशॉट ImageView में प्रदर्शित होगा और पृष्ठभूमि ग्रे हो जाएगी।

नोट: drawingCache API डिप्रिकेटेड है। आधुनिक ऐप्स के लिए PixelCopy API या View.drawToBitmap() (API 29+) का उपयोग करने की सलाह दी जाती है।
  1. - Android में प्रोग्रामेटिकली स्क्रीनशॉट कैसे लें?

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

  1. Android में प्रोग्रामेटिकली स्क्रीनशॉट कैसे लें? Android में प्रोग्रामेटिकली स्क्रीनशॉट कैसे लें?

    यह उदाहरण दर्शाता है कि Android ऐप में व्यू का स्क्रीनशॉट प्रोग्रामेटिकली कैसे लिया जाए और उसे ImageView में दिखाया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML res/layout/activity_main.xml - बटन और ImageView के साथ: <?xml version=1.0 encoding=utf-8?&

  1. एंड्रॉइड में कैमरे से प्रोग्रामेटिकली फोटो कैसे लें? एंड्रॉइड में कैमरे से प्रोग्रामेटिकली फोटो कैसे लें?

    एंड्रॉइड में कैमरा इंटेंट से फोटो लेना — पूरा गाइड एंड्रॉइड में फोटो लेने के दो मुख्य तरीके हैं: इंटेंट (ACTION_IMAGE_CAPTURE) — सिस्टम कैमरा ऐप खुलता है, यूज़र फोटो लेता है, रिजल्ट आपके ऐप को मिलता है। **सबसे सरल, परमिशन कम, कोई UI कोड नहीं।** CameraX / Camera2 API — अपने ऐप में कैमरा प्रीव्यू, कं