android 6.0 需要手动访问权限
if (ContextCompat.checkSelfPermission(ShareCodeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ShareCodeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 54);
Log.i("-->", "权限申请");
}else
{
screenshot();
}
/** * 获取屏幕 */ private void screenshot() { // 获取屏幕 View dView = getWindow().getDecorView(); dView.setDrawingCacheEnabled(true); dView.buildDrawingCache(); Bitmap bmp = dView.getDrawingCache(); File file = PathGetUtils.getFile(this); if (bmp != null) { try { if (file.exists()) { file.delete(); } FileOutputStream os = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 100, os); os.flush(); os.close(); String mUri = MediaStore.Images.Media.insertImage(getContentResolver(), file.getPath(), "name", null); if (mUri!=null) { Toast.makeText(ShareCodeActivity.this,"截图成功",Toast.LENGTH_SHORT).show(); // 最后通知图库更新 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(new File(mUri)); intent.setData(uri); sendBroadcast(intent); }else { Toast.makeText(ShareCodeActivity.this,"截图失败",Toast.LENGTH_SHORT).show(); } } catch (Exception e) { } } }