Android截图<包括Alertdiaog>

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

    }

}
时间: 2024-12-14 12:58:18

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

Android截图

Android中截图很好实现,从开发文档中,可以看到View有一个接口getDrawingCache(),这个接口可以获取View在调用这个接口时的位图图像Bitmap. 截图是抓取View在某一个时刻的图像,包含了addView到这个View的所有子View的图像,比如在截取Activity时,图像是不会包含浮现在activity上方的对话框的 下面的代码是截取图像并且经过http post接口上传到服务器的例子,截图并上传的核心代码如下: Screenshot.java package co

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

Android截图命令screencap 查看帮助命令 [email protected] ~$ adb shell screencap -v screencap: invalid option -- v usage: screencap [-hp] [-d display-id] [FILENAME] -h: this message -p: save the file as a png. -d: specify the display id to capture, default 0. If

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

◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6113059.html 需求 这两天遇到这样一个事情,因为某测试任务,需要在操作过程中连续的截图,最终分析截图.之前同事用的工具兼容性特别的不好,需要root,并且只适配固定几个版本的机型,因此我决定自己实现一个.首先最先想到的就是使用Uiautomator 1中自带的API来截图. 我们看下Uiautomator 1.0中提供的截图相关的API是什么样子的

vlc android 截图和录制视频(vlc0.9.9)

1.下载编译vlc 0.9.9 参考https://wiki.videolan.org/AndroidCompile/ 我的系统是Ubuntu 14.04 64位 2.在项目根目录../android,打上补丁 git apply *.patch 3.在../android/vlc,打上补丁 4.重新编译即可 要截图和录制视频的时候 只要使用LibVLC.java中的下面接口 public boolean takeSnapShot(String file, int width, int heig

Android截图截取弹框AlertDialog

1:效果如图 2:权限 <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permissi

关于android截图的一些方法

这里只记录一些链接,对于我的需求,只需要, public static String createScreenShot(View v)     {        //测试截屏功能    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd_HH-mm-ss",Locale.US);    String dirPath=Mapplication.SDPATH+Constants.QZ_PATH;    File dirFile=

Android截图命令screencap

查看帮助命令 [email protected] ~$ adb shell screencap -v screencap: invalid option -- v usage: screencap [-hp] [-d display-id] [FILENAME] -h: this message -p: save the file as a png. -d: specify the display id to capture, default 0. If FILENAME ends with .

android 截图,视频录制命令 screencap screenrecord screenshot

时间限制视频录制: screenrecord  --time-limit 20 /sdcard/demo.mp4 抓屏: screencap -p /sdcard/screen.png screenshot /sdcard/screen.png 视频录制命令screenrecord # screenrecord --help Usage: screenrecord [options] <filename> Records the device's display to a .mp4 file.

android 截图保存位置不在已选择的“SD卡”中

修改GlobalScreenshot.java的saveImageInBackgroundTask方法: String imageDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getAbsolutePath(); 1.JB2修改为: String imageDir = StorageManager.getDefaultPath() + '/' + Environment.D