安卓 : taking Screenshot of the selected area programmatically

标签 android screenshot

我的代码如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button upload = (Button) findViewById(R.id.screeshotdButton);

    upload.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            folderCheck();
        }
    });
}
private void folderCheck(){
    File folder = new File(Environment.getExternalStorageDirectory() + "/cloze_screenshots");
    boolean success = true;
    // If the folder cloze not exist, create one
    if (!folder.exists()) {
        success = folder.mkdir();       
    }else{
        ScreenShot();
    }
    // If mkdir successful
    if (success) {
        ScreenShot();       
    } else {
        Log.e("mkdir_fail","QQ"); 
    }

}

private void ScreenShot(){

    String filePath = Environment.getExternalStorageDirectory()+ "/cloze_screenshots/temp.png"; 

    // create bitmap screen capture
    Bitmap bitmap;
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    OutputStream fout = null;
    File imageFile = new File(filePath);

    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
        fout.flush();
        fout.close();
        Toast.makeText(this, "Success", Toast.LENGTH_LONG).show();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}  

此代码可以截取全屏屏幕截图,但我想在按下按钮后以编程方式截取特定区域(例如,屏幕上的左侧 block )的屏幕截图。 任何代码或建议将不胜感激。

最佳答案

您可以将内容包装在一个布局中,例如 LinearLayout,然后按照上面的代码使用包装布局上的方法进行屏幕截图。

 Bitmap bitmap;
 ViewGroup v1 = findViewById(R.id.layout_id);
 v1.setDrawingCacheEnabled(true);
 bitmap = Bitmap.createBitmap(v1.getDrawingCache());
 v1.setDrawingCacheEnabled(false);

关于安卓 : taking Screenshot of the selected area programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26190852/

相关文章:

java - 仅 Galaxy Note 2 和 3 中的 OutofMemoryError

php - 如何通过php从mysql数据库中获取所有产品类别并返回json数组

android - 获取屏幕截图时出现意外错误 - java.lang.nullpointerexception

php - 在wkhtmltoimage中截图时如何指定大小?

java - 签名 APK 的配置出现错误 "configuration is still incorrect"

javascript - Nativescript 裸 webview 似乎不起作用

java - 如何通过 Selenium 和 Java 使用 AShot 库截取整页截图

javascript - Playwright 中的 GDPR cookie 弹出窗口

git - 显示存储在 GitHub 和本地不同分支中的 GitHub README 屏幕截图

php - 从两个表中提取