简体   繁体   English

如何在Android中以编程方式拍摄当前屏幕的屏幕截图?

[英]How to take screen shot of current screen in android programmaticaly?

我已经用一些textviews和listview设计了我的android布局,加载屏幕后,我只想拍摄该布局的屏幕快照,我必须将其保存在我的设备上。是否可以。

Bitmap bitmap;
View v1 = findViewById(R.id.rlid);// get ur root view id
v1.setDrawingCacheEnabled(true); 
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

For saving 为了节省

 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
 bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
 File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "test.jpg")
 f.createNewFile();
 FileOutputStream fo = new FileOutputStream(f);
 fo.write(bytes.toByteArray()); 
 fo.close();

dont forget to give permission 不要忘记给予许可

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

From How to programatically take a screenshot on Android? 如何以编程方式在Android上截取屏幕截图?

Here is the sample code: 这是示例代码:

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   
// 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;
imageFile = new File(mPath);

try {
  fout = new FileOutputStream(imageFile);
  bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
  fout.flush();
  fout.close();

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

For this you need this permission in manifest 为此,您需要清单中的此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

请检查此链接: http : //www.b2creativedesigns.com/how-to-take-screenshot-programmatically.php

In short, you will have to take screenshot of the Parent View and save it programtically.

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

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