androidandroid-studiodevicecaptureinfo

Can I get a captured image from the Android device information screen?


Intent setting = new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS);

You can display the Android device information screen with the above code.

But when I run the code that captures the screen and saves the image, I get nothing.

I think this is for security reasons, but I need that feature. Is it possible in another way?

private void takeScreenshot(View v1) {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

        try {
            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(mPath);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

This is the capture code I tried.

v1.setDrawingCacheEnabled(true);

put the above code

v1.getDrawingCache()

I don't get anything from the above code.

All actions should be performed automatically, without click events such as buttons.


Solution

  • as per my understanding you want to take screenshot of screen , so try below code,

    in xml , step 1)

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click on button to take screenshot"
    android:textColor="#000"
    android:textSize="20dp"
    android:textStyle="bold" />
    
    <Button
    android:id="@+id/clickme"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@color/colorPrimary"
    android:text="Click Me" />
    

    and in activity step 2) initiate button and call onclick and add below,

    final MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.beep);
    click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "You just Captured a Screenshot," + Toast.LENGTH_SHORT).show();
            screenshot(getWindow().getDecorView().getRootView(), "result");
    
            mediaPlayer.start();
        }
    });
    

    step 3) declare screenshot function,

    protected static File screenshot(View view, String filename) {
        Date date = new Date();
    
    // Here we are initialising the format of our image name
        CharSequence format = android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", date);
        try {
    // Initialising the directory of storage
            String dirpath = Environment.getExternalStorageDirectory() + "";
            File file = new File(dirpath);
            if (!file.exists()) {
                boolean mkdir = file.mkdir();
            }
    
    // File name
            String path = dirpath + "/" + filename + "-" + format + ".jpeg";
            view.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);
            File imageurl = new File(path);
            FileOutputStream outputStream = new FileOutputStream(imageurl);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
            outputStream.flush();
            outputStream.close();
            return imageurl;
    
        } catch (FileNotFoundException io) {
            io.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    step 4) most remember point ask user to storage permission(WRITE_EXTERNAL_STORAGE). hope it's help.