简体   繁体   English

我可以从 Android 设备信息屏幕获取捕获的图像吗?

[英]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.您可以使用上述代码显示 Android 设备信息屏幕。

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.我没有从上面的代码中得到任何东西。

as per my understanding you want to take screenshot of screen, so try below code,根据我的理解,你想截取屏幕截图,所以试试下面的代码,

in xml, step 1)在 xml 中,步骤 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,并在活动步骤 2) 启动按钮并调用 onclick 并在下面添加,

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,步骤3)声明截图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).第 4 步)最记住的一点是询问用户存储权限(WRITE_EXTERNAL_STORAGE)。 hope it's help.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM