码迷,mamicode.com
首页 > 移动开发 > 详细

Android截图<包括Alertdiaog>

时间:2017-02-12 19:05:06      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:try   rom   getpath   tar   alert   err   flush   system   class   

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>

标签:try   rom   getpath   tar   alert   err   flush   system   class   

原文地址:http://www.cnblogs.com/galibujianbusana/p/6391470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!