1.使用的系统内部的截屏方法……

2.

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
View view1;
int x=1;
private static final int REQUEST_MEDIA_PROJECTION = 1;
private MediaProjectionManager projectionManager;
boolean screenCapture = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void click(View view) { AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
localBuilder.setTitle("自定义列表对话框").setIcon(R.mipmap.ic_launcher);
view1=getLayoutInflater().inflate(R.layout.layout, null);
localBuilder.setView(getLayoutInflater().inflate(R.layout.layout, null));
localBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
{
/**
*
* 操作
* */
// 获取屏幕
screenCapture = true;
x=1;
takeScreenshot2();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} }
}).setNegativeButton("取消", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
{
/**
*
* 操作
* */ }
}).create().show(); } public void takeScreenshot2() {
try {
projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(
projectionManager.createScreenCaptureIntent(),
REQUEST_MEDIA_PROJECTION);
} catch (Exception e) {
e.printStackTrace();
}
} @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_MEDIA_PROJECTION && screenCapture==true) {
/**
*
*/ try {
final int mWidth = getWindowManager().getDefaultDisplay().getWidth();
final int mHeight = getWindowManager().getDefaultDisplay().getHeight();
final ImageReader mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int mScreenDensity = metrics.densityDpi;
final MediaProjection mProjection = projectionManager.getMediaProjection(-1, data);
Log.i(TAG, "onActivityResult2: "+screenCapture+"**"+x);
final VirtualDisplay virtualDisplay = mProjection.createVirtualDisplay("screen-mirror",
mWidth, mHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mImageReader.getSurface(), null, null);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
if(x!=1){
return;
}
else{
try {
Log.i(TAG, "onActivityResult3: "+screenCapture+"**"+x);
/**
* 去掉--->mProjection.stop(); 会出现不停的截图现象
*/
x=2;
mProjection.stop();
Image image = mImageReader.acquireLatestImage();
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int offset = 0;
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mWidth;
Bitmap bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
image.close();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
String strDate = dateFormat.format(new java.util.Date());
/**
* 截图的保存路径……
*/
String pathImage = Environment.getExternalStorageDirectory().getPath() + "/";
/**
* 截图名称……
*/
String nameImage = pathImage + strDate + ".png";
if (bitmap != null) {
try {
File fileImage = new File(nameImage);
if (!fileImage.exists()) {
fileImage.createNewFile();
}
FileOutputStream out = new FileOutputStream(fileImage);
if (out != null) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Toast.makeText(MainActivity.this, "图片保存成功!", Toast.LENGTH_SHORT).show();
Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(fileImage);
media.setData(contentUri);
getApplicationContext().sendBroadcast(media);
screenCapture = false;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(MainActivity.this, "cannot get phone's screen", Toast.LENGTH_SHORT).show(); }
} catch (Exception e) {
e.printStackTrace();
}
} }
}, null);
} catch (Exception e) {
e.printStackTrace();
}
/**
*
*/
} } }

Android截图<包括Alertdiaog>的更多相关文章

  1. Android截图

    Android截图很好的实现,从文档的发展,查看View有一个接口getDrawingCache(),这个接口可以得到View当调用这个接口的位图图像Bitmap. 抓取截图View在图像的某一个时刻 ...

  2. Android截图命令screencap与视频录制命令screenrecord

    Android截图命令screencap 查看帮助命令 bixiaopeng@bixiaopeng ~$ adb shell screencap -v screencap: invalid optio ...

  3. 【Android测试】Android截图的深水区

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6113059.html 需求 这两天遇到这样一个事情,因为某 ...

  4. Python实现对Android截图

    背景: 测试过程中,总是需要对Android设备进行截图,然后在截图中标注问题描述: 手动方式: 1.使用adb scrrencap /sdcard/screen.png 命令对Android设备进行 ...

  5. Android截图截取弹框AlertDialog

    1:效果如图 2:权限 <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" ...

  6. 关于android截图的一些方法

    这里只记录一些链接,对于我的需求,只需要, public static String createScreenShot(View v)     {        //测试截屏功能    SimpleD ...

  7. Android截图命令screencap

    查看帮助命令 bixiaopeng@bixiaopeng ~$ adb shell screencap -v screencap: invalid option -- v usage: screenc ...

  8. Android应用截图和SurfaceView截图问题总结

          最近在做android截图应用的过程遇到很多问题,接触了好些截图方法,但是还是不能实现SufaceView截图功能.今天就把我尝试过的方法总结下,希望把我惨痛的经历写出来后能够帮助到要做此 ...

  9. Android之高效率截图

    本文来自网易云社区 作者:孙圣翔 在一张Android手机上截图有好多办法,为了能够高效率的截图,我几乎把所有的方法都尝试了一般.走了好多路,也遇到了好多的问题. 只是想记录下这其中的不容易. 下面所 ...

随机推荐

  1. Eclipse中添加对Python的中文支持

    原文链接:http://down.51cto.com/data/751371 首先要确保eclipse编辑器环境的编码为utf8,这个是大前提:其次如果py文件中含有中文字符的话,需要在py文件中对编 ...

  2. Eigen库对齐问题:declspec(align('16')) 的形参将不被对齐

    一:错误提示:error C2719: '_Val': formal parameter with __declspec(align('16')) won't be aligned 英文提示:erro ...

  3. day09网络编程

    一 操作系统基础 操作系统:(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才 ...

  4. android学习路线总结

    感谢安辉作者,学习路线  https://www.cnblogs.com/yishaochu/p/5436094.html https://www.cnblogs.com/jycboy/p/60666 ...

  5. Java 分布式事务

    0 引言 本文主要介绍java中分布式事务以及对应的解决方案. 1 分布式事务产生的原因 1.1 数据库分库分表 当数据库单表一年产生的数据超过1000W,那么就要考虑分库分表,具体分库分表的原理在此 ...

  6. ZooKeeper概念

    这可能是把ZooKeeper概念讲的最清楚的一篇文章 相信大家对 ZooKeeper 应该不算陌生,但是你真的了解 ZooKeeper 是什么吗?如果别人/面试官让你讲讲 ZooKeeper 是什么, ...

  7. leetCode笔记--binary tree

    993. Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each dept ...

  8. Btrfs 文件系统

    btrfs文件系统简介  btrfs文件系统:技术预览版(Centos7)  Btrfs(B-tree.Butter FS.Better FS),GPL授权,Orale2007提出是想用来取代Ext文 ...

  9. Python-基础-day3

    基础数据类型 1.什么是数据类型? 我们人类可以很容易的分清数字与字符的区别,但是计算机并不能呀,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,“汉”是文字,否则它是分不清1 ...

  10. OGG For Bigdata To Kafka同步问题处理

    一.问题报错 -- :: ERROR OGG- Java or JNI exception: oracle.goldengate.util.GGException: Error detected ha ...