如何以编程方式拍摄Android中打开的当前活动的屏幕截图?

如何以编程方式拍摄Android中打开的当前活动的屏幕截图?,android,Android,使用手机和手机主屏幕时拍摄屏幕截图。 当我使用手机屏幕上的信息、所有应用程序和任何东西时,也可以使用 在排序中,拍摄移动运行屏幕的图像:在清单中添加以下权限 1-创建布局 2-创建活动 导入android.graphics.Bitmap; 导入android.graphics.drawable.BitmapDrawable; 导入android.os.Bundle; 导入android.os.Environment; 导入android.support.v7.app.AppActivity; 导

使用手机和手机主屏幕时拍摄屏幕截图。 当我使用手机屏幕上的信息、所有应用程序和任何东西时,也可以使用


在排序中,拍摄移动运行屏幕的图像:

在清单中添加以下权限
1-创建布局
2-创建活动
导入android.graphics.Bitmap;
导入android.graphics.drawable.BitmapDrawable;
导入android.os.Bundle;
导入android.os.Environment;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.Toast;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
公共类MainActivity扩展了AppCompatActivity{
按钮btn_屏幕截图;
int i=0;
ImageView imgv_显示屏幕截图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgv_showscreenshot=(ImageView)findViewById(R.id.showscreenshot);
btn_屏幕截图=(按钮)findViewById(R.id.btn);
btn_屏幕截图.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
视图=findViewById(R.id.relativelayout);
view.setDrawingCacheEnabled(true);
位图Bitmap=view.getDrawingCache();
BitmapDrawable BitmapDrawable=新的BitmapDrawable(位图);
//将屏幕截图bitmapdrawable设置为imageview
imgv_显示屏幕截图。可设置背景图(可位图绘制);
if(Environment.MEDIA_MOUNTED.equals)(环境
.getExternalStorageState())){
//我们检查外部存储是否可用,否则
//使用Toast消息向用户显示错误消息
文件sdCard=Environment.getExternalStorageDirectory();
文件目录=新文件(sdCard.getAbsolutePath()
+“/截图”);
mkdirs()目录;
字符串filename=“屏幕截图”+i+“.jpg”;
File yourFile=新文件(目录、文件名);
while(yourFile.exists()){
i++;
filename=“屏幕截图”+i+“.jpg”;
yourFile=新文件(目录、文件名);
}
如果(!yourFile.exists()){
if(directory.canWrite()){
试一试{
FileOutputStream out=新的FileOutputStream(
你的档案,没错);
bitmap.compress(bitmap.CompressFormat.PNG,90,
),;
out.flush();
out.close();
Toast.makeText(
这个,,
“文件导出到/sdcard/ScreenShots/screenshot”
+i+“.jpg”,
吐司。长度(短)。show();
i++;
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}否则{
Toast.makeText(MainActivity.this,
“对不起,SD卡在您的设备中不可用!”,
吐司。长度(短)。show();
}
}
});
}
}
3-添加对清单文件的权限

使用此选项。我所做的是,当点击按钮时,它以与截图截图类似的方式保存整个布局

     linearLayout = (LinearLayout) findViewById(R.id.screenshots); //say   for eg: this is the main layout id wich holds everything(images etc)
     //use a button to call this method.        
     private void saveLayout() {
       // View v1 = getWindow().getDecorView().getRootView();
       View v1 = linearLayout.getRootView();
       v1.setDrawingCacheEnabled(true);
       myBitmap = v1.getDrawingCache();
       if (myBitmap != null) {
          Toast.makeText(MainScreen.this, "Bitmap not null",
          Toast.LENGTH_SHORT).show();
          saveBitmap(myBitmap);
       } else {
          Toast.makeText(MainScreen.this, "Bitmap null",Toast.LENGTH_SHORT).show();
       }
    }

   private void saveBitmap(Bitmap bitmap) {

    try {
        File mFolder = new File(getFilesDir() + "/nmc"); //give a name for the folder
        File imagePath = new File(mFolder + "screenshot.png"); 
        if (!mFolder.exists()) {
            mFolder.mkdir();
        }
        if (!imagePath.exists()) {
            imagePath.createNewFile();
        }
        FileOutputStream fos=null;

        fos = new FileOutputStream(imagePath);
        // bitmap.compress(CompressFormat.PNG, 100, fos);
        bitmap.compress(Bitmap.CompressFormat.PNG, 60, fos);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        String encodedByte = Base64.encodeToString(byteArray,Base64.DEFAULT);
        Log.e("encodeByte", encodedByte);
        fos.flush();
        fos.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), bitmap,"Screen", "screen");

    } catch (FileNotFoundException e) {
        Log.e("no file", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("io", e.getMessage(), e);
    }

}

为什么其他答案中有这么多代码行?该方法的核心仅为3行:

  public Bitmap takeScreenshot() {
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
  }
然后在按钮单击事件中:

        bitmap = takeScreenshot();
        CommonUtils.saveImage(bitmap);
顺便说一句,
通用代码如下:

  public static void saveImage(Bitmap bitmap) {

    File imageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
      + "DCIM" + File.separator + Constants.APP_NAME);
    boolean mkdirSuccess = true;
    if (!imageDir.exists()) {
      mkdirSuccess = imageDir.mkdir();
    }
    if (!mkdirSuccess) {
      Log.e("== saveImage", "mkdir FAILED!");
      return;
    }
    String fileName = System.currentTimeMillis() + ".jpg";
    File file = new File(imageDir, fileName);
    try {
      FileOutputStream fileOutputStream = new FileOutputStream(file);

      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
      fileOutputStream.flush();
      fileOutputStream.close();

      Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
      intent.setData(Uri.fromFile(file));
      BaseApplication.getContext().sendBroadcast(intent);

    } catch (IOException e) {
      e.printStackTrace();
    }

  }

我想你可以试着把你需要的视图绘制成位图,然后保存它。谷歌一下就可以了。有很多教程可以截图。记住,你只能从你的应用程序中收到截图。没有底部或状态栏,而且我记得也没有打开的菜单。的可能副本在APK 25中无效
        bitmap = takeScreenshot();
        CommonUtils.saveImage(bitmap);
  public static void saveImage(Bitmap bitmap) {

    File imageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
      + "DCIM" + File.separator + Constants.APP_NAME);
    boolean mkdirSuccess = true;
    if (!imageDir.exists()) {
      mkdirSuccess = imageDir.mkdir();
    }
    if (!mkdirSuccess) {
      Log.e("== saveImage", "mkdir FAILED!");
      return;
    }
    String fileName = System.currentTimeMillis() + ".jpg";
    File file = new File(imageDir, fileName);
    try {
      FileOutputStream fileOutputStream = new FileOutputStream(file);

      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
      fileOutputStream.flush();
      fileOutputStream.close();

      Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
      intent.setData(Uri.fromFile(file));
      BaseApplication.getContext().sendBroadcast(intent);

    } catch (IOException e) {
      e.printStackTrace();
    }

  }