sdk 截屏之调用系统截屏
来源:互联网 发布:linux 禁止ip连接 编辑:程序博客网 时间:2024/06/07 12:10
前天写了一篇截图(截屏)–未调用系统截屏管理,今天介绍一篇关于利用系统截屏的
其一:申请的权限如下:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
其二:关于TargetSdkVersion的版本 大小–最好选择在23以下(给用户去选择申请权限)
其三:其思想和上一篇文章是一样的 ,这里就不介绍了,直接贴代码
主体部分:
private ImageView mImageCupView; private MediaProjectionManager mMediaProjectionManager; private int mScreenWidth; private int mScreenHeight; private int mScreenDensity; private ImageReader mImageReader; private MediaProjection mMediaProjection; private VirtualDisplay mVirtualDisplay; public static final int REQUEST_MEDIA_PROJECTION = 18; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取当前屏幕的像素点 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); mScreenDensity = metrics.densityDpi; mScreenWidth = metrics.widthPixels; mScreenHeight = metrics.heightPixels; //5 表示接受的权限的最多次数(拒绝5次就会崩溃) mImageReader = ImageReader.newInstance(mScreenWidth, mScreenHeight, PixelFormat.RGBA_8888, 5); //检查其版本号 requestCapturePermission(); mImageCupView = (ImageView) findViewById(R.id.cupView); findViewById(R.id.text).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mMediaProjection == null) { //判断是否获取到权限,否则开启重新开启权限 Toast.makeText(MainActivity.this,"您必须接受开启权限",Toast.LENGTH_SHORT).show(); requestCapturePermission(); } startScreenShot(); } }); } private void startScreenShot() { Handler handler1 = new Handler(); handler1.postDelayed(new Runnable() { public void run() { //start virtual virtualDisplay(); } }, 5); handler1.postDelayed(new Runnable() { public void run() { //capture the screen startCapture(); } }, 30); } private void startCapture() { Image image = mImageReader.acquireLatestImage(); if (image == null) { //开始截屏 startScreenShot(); } else { //保存截屏 SaveTask mSaveTask = new SaveTask(); AsyncTaskCompat.executeParallel(mSaveTask, image); } } public class SaveTask extends AsyncTask<Image, Void, Bitmap> { @Override protected Bitmap doInBackground(Image... params) { if (params == null || params.length < 1 || params[0] == null) { return null; } Image image = params[0]; int width = image.getWidth(); int height = image.getHeight(); final Image.Plane[] planes = image.getPlanes(); final ByteBuffer buffer = planes[0].getBuffer(); //每个像素的间距 int pixelStride = planes[0].getPixelStride(); //总的间距 int rowStride = planes[0].getRowStride(); int rowPadding = rowStride - pixelStride * width; Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888); bitmap.copyPixelsFromBuffer(buffer); bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height); image.close(); File fileImage = null; if (bitmap != null) { try { fileImage = new File(FileUtil.getScreenShotsName(getApplicationContext())); if (!fileImage.exists()) { fileImage.createNewFile(); } FileOutputStream out = new FileOutputStream(fileImage); if (out != null) { bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); //发送广播给相册--更新相册图片 Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri contentUri = Uri.fromFile(fileImage); media.setData(contentUri); sendBroadcast(media); } } catch (FileNotFoundException e) { e.printStackTrace(); fileImage = null; } catch (IOException e) { e.printStackTrace(); fileImage = null; } } if (fileImage != null) { return bitmap; } return null; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); //预览图片 if (bitmap != null) { Toast.makeText(MainActivity.this,"截图已经成功保存到相册",Toast.LENGTH_SHORT).show(); //直接设置动画效果 setAnimation(bitmap); } } } private void setAnimation(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); ViewGroup.LayoutParams layoutParams = mImageCupView.getLayoutParams(); layoutParams.height = height; layoutParams.width = width; //mImagerCupView是布局文件中创建的 mImageCupView.setLayoutParams(layoutParams); mImageCupView.setImageBitmap(bitmap); AnimatorSet animatorSet = new AnimatorSet(); ObjectAnimator animatory = ObjectAnimator.ofFloat(mImageCupView, "scaleY", 1.0f, 0.0f); ObjectAnimator animatorx = ObjectAnimator.ofFloat(mImageCupView, "scaleX", 1.0f, 0.0f); animatorSet.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { //后面可以继续接着写其他活动,如页面的跳转等等 } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); animatorSet.setDuration(500); animatorSet.play(animatory).with(animatorx);//两个动画同时开始 animatorSet.start(); } private void virtualDisplay() { if(mMediaProjection != null) { mVirtualDisplay = mMediaProjection.createVirtualDisplay("screen-mirror", mScreenWidth, mScreenHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null); } } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void requestCapturePermission() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Toast.makeText(this, "不能截屏", Toast.LENGTH_LONG).show(); return; } //获取截屏的管理器 mMediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE); startActivityForResult(mMediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_MEDIA_PROJECTION: if (resultCode == RESULT_OK && data != null) { mMediaProjection = mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK, data); } break; } } private void tearDownMediaProjection() { if (mMediaProjection != null) { mMediaProjection.stop(); mMediaProjection = null; } } private void stopVirtual() { if (mVirtualDisplay == null) { return; } mVirtualDisplay.release(); mVirtualDisplay = null; } // 结束后销毁 @Override public void onDestroy() { super.onDestroy(); stopVirtual(); tearDownMediaProjection(); }
工具类:如下
public class FileUtil { //系统保存截图的路径 public static final String SCREENCAPTURE_PATH = "ScreenCapture" + File.separator + "Screenshots" + File.separator; public static final String SCREENSHOT_NAME = "Screenshot"; public static String getAppPath(Context context) { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { return Environment.getExternalStorageDirectory().toString(); } else { return context.getFilesDir().toString(); } } public static String getScreenShots(Context context) { StringBuffer stringBuffer = new StringBuffer(getAppPath(context)); stringBuffer.append(File.separator); stringBuffer.append(SCREENCAPTURE_PATH); File file = new File(stringBuffer.toString()); if (!file.exists()) { file.mkdirs(); } return stringBuffer.toString(); } public static String getScreenShotsName(Context context) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); String date = simpleDateFormat.format(new Date()); StringBuffer stringBuffer = new StringBuffer(getScreenShots(context)); stringBuffer.append(SCREENSHOT_NAME); stringBuffer.append("_"); stringBuffer.append(date); stringBuffer.append(".png"); return stringBuffer.toString(); }}
以上,就不做解释了
阅读全文
0 0
- sdk 截屏之调用系统截屏
- sdk之截屏
- Android SDK之系统权限
- 调用系统锁屏
- 001-SDK框架之Unity游戏调用SDK
- Nachos之系统调用
- unix系统之系统调用
- 调用Android系统应用时解决SDK不兼容问题
- 文件编程之系统调用
- 文件操作之系统调用
- linux系统调用之ftok()
- Android系统调用之Intent
- Linux内核之系统调用
- Linux内核之系统调用
- Linux架构之系统调用
- 系统调用之时间开销
- 文件操作之系统调用
- Linux系统调用之SYSCALL_DEFINE
- linux字符驱动之poll机制按键驱动
- 1 自然语言处理中文语料与词汇资源从哪里获取
- 据说大部分资深程序员都必须掌握的工具
- 2017校招全国统一模拟考试(第五场)JavaScript解法记录(题目来源-->牛客网)
- Android Studio常用快捷键
- sdk 截屏之调用系统截屏
- Jenkins持续集成工具使用说明
- 'module' object has no attribute 'xfeatures2d'(已解决)
- SSM+maven+sqlserver+mybatis-generator逆向生成类
- 思考练习
- Windows环境下在Apache24上部署多个django项目(多端口)
- jdk环境变量的配置
- 【Java】vmware克隆
- Onvif协议-设备搜索