截屏方法
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.Display;
import android.view.View;
import java.io.IOException;
/**
* @param
* @Comments : 截屏
* @Author : Lampo
* @CreateDate : 2018/11/5 15:17
* @ModifiedBy : Lampo
* @ModifiedDate : 2018/11/5 15:17
* @Modified : 6.0以上使用前要申请权限 Manifest.permission.WRITE_EXTERNAL_STORAGE
* Manifest.permission.READ_EXTERNAL_STORAGE
*/
public class ScreenShotsUtils {
public static boolean screenShots(Activity activity) throws IOException {
return screenShots(activity, 0, 0);
}
public static boolean screenShotsWidth(Activity activity, int width) throws IOException {
return screenShots(activity, width, 0);
}
public static boolean screenShotsHeight(Activity activity, int height) throws IOException {
return screenShots(activity, 0, height);
}
public static boolean screenShots(Activity activity, int width, int height) throws IOException {
boolean flag = false;
// 获取windows中最顶层的view
View view = activity.getWindow().getDecorView();
view.buildDrawingCache();
// 获取状态栏高度
Rect rect = new Rect();
view.getWindowVisibleDisplayFrame(rect);
int statusBarHeights = rect.top;
Display display = activity.getWindowManager().getDefaultDisplay();
// 获取屏幕宽和高
int widths = display.getWidth();
int heights = display.getHeight();
// 允许当前窗口保存缓存信息
view.setDrawingCacheEnabled(true);
// 去掉状态栏
Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), width,
statusBarHeights + height, widths - width, heights - statusBarHeights - height);
// 销毁缓存信息
view.destroyDrawingCache();
if (bmp != null) {
final String path = FileUtils.getImageDownloadPath(activity);
flag = FileUtils.saveFile(bmp, path);
}
return flag;
}
/**
* 保存一个view的截图
*
* @param context
* @param view
* @return
*/
public static boolean screenShots(Context context, View view) {
boolean flag = false;
Bitmap bitmap = getCacheBitmap(view);
if (bitmap != null) {
final String path = FileUtils.getImageDownloadPath(context);
flag = FileUtils.saveFile(bitmap, path);
}
return flag;
}
/**
* 获取一个 View 的缓存视图
*
* @param view
* @return
*/
public static Bitmap getCacheBitmap(View view) {
final boolean drawingCacheEnabled = true;
view.setDrawingCacheEnabled(drawingCacheEnabled);
view.buildDrawingCache(drawingCacheEnabled);
final Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap;
if (drawingCache != null) {
bitmap = Bitmap.createBitmap(drawingCache);
view.setDrawingCacheEnabled(false);
} else {
bitmap = null;
}
return bitmap;
}
}
保存到路径
/**
* 随机生成一个文件,用于存放下载的图片
*
* @param context Context
* @return
*/
public static String getImageDownloadPath(Context context) {
String imageRootDir = getImageDownloadDir(context);
File dir = new File(imageRootDir);
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = System.currentTimeMillis() + ".jpg";
return dir + File.separator + fileName;
}
/**
* 获取下载图片文件存放的目录
*
* @param context Context
* @return SDCard卡或者手机内存的根路径
*/
public static String getImageDownloadDir(Context context) {
return getRootDir(context) + IMAGE_DOWNLOAD_IMAGES_PATH;
}
/**
* 获取SDCard卡或者手机内存的根路径(优先获取SDCard卡的根路径)
*
* @param context Context
* @return SDCard卡或者手机内存的根路径
*/
public static String getRootDir(Context context) {
String rootDir;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + ROOT_DIR_PATH;
} else {
rootDir = context.getApplicationContext().getCacheDir().getAbsolutePath() + ROOT_DIR_PATH;
}
return rootDir;
}
保存图片并且通知相册进行更新
/**
* 保存文件
*
* @param bm
* @param fileName
* @throws IOException
*/
public static boolean saveFile(Bitmap bm, String fileName) {
boolean flag = false;
File myCaptureFile = new File(fileName);
FileOutputStream bos = null;
try {
bos = new FileOutputStream(myCaptureFile);
flag = bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Uri uri = Uri.fromFile(myCaptureFile);
AICashApplication.getApplication().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
return flag;
}