如何在android programmaticaly中拍摄当前屏幕的屏幕截图?

时间:2015-05-26 11:06:36

标签: android

我设计了我的Android布局,其中包含一些textview和listview,加载屏幕后我只想拍摄该布局的屏幕截图,我必须将其保存在我的设备上。是否可能。

3 个答案:

答案 0 :(得分:1)

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

保存

 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();

别忘了给予许可

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

答案 1 :(得分:0)

来自How to programatically take a screenshot on Android?

以下是示例代码:

// 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();
}

为此,您需要在清单中使用此权限

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

答案 2 :(得分:-1)

请检查此链接: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.