How to take screenshot for Android Surface view

帅比萌擦擦* 提交于 2019-12-04 17:10:43
mContentView.setDrawingCacheEnabled(true);
mContentView.buildDrawingCache(true);

                Bitmap b = Bitmap.createBitmap(mContentView.getDrawingCache());
                mContentView.setDrawingCacheEnabled(false);


                //Save bitmap to ur sdcard

                File mFolder = new File( Environment.getExternalStorageDirectory().toString()+ "/xxx");
                File mCapture = new File( mFolder.getAbsolutePath()+ "/Captured");

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS");
                String dateString = formatter.format(new java.util.Date());

                String fileName = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS'_xxx.jpg'").format(new Date());
                File imgFile = new File(mCapture.getAbsolutePath(), fileName);

                String extr = Environment.getExternalStorageDirectory().toString() +   File.separator + "xxx/Captured";

                FileOutputStream fos = null;
                try {
                    if (!mFolder.exists()) {
                        mFolder.mkdir();
                    }
                    if (!mCapture.exists()) {
                        mCapture.mkdir();
                    }
                    if (!imgFile.exists()) {
                        imgFile.createNewFile();
                    }
                    fos = new FileOutputStream(imgFile);
                    b.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    fos.flush();
                    fos.close();
                    MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), b, "Screen", "screen");
                }catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



                final Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("image/jpg");
                final File photoFile = new File(extr,fileName);
                shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
                startActivity(Intent.createChooser(shareIntent, "Share image using"));

here mContentView is the parent view of your surface view. apply this and let me know if you are not getting image screen shots.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!