直接上關鍵代碼:
ScreenShotUtils 工具類代碼:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
/**
* @Description: 進行截屏工具類
* @author wjj
* @time 2013/09/29
*/
public class ScreenShotUtils {
private static Context context;
/**
* 進行截取屏幕
* @param pActivity
* @return bitmap
*/
public static String takeScreenShot(Activity pActivity)
{
context = pActivity;
Bitmap bitmap=null;
View view=pActivity.getWindow().getDecorView();
// 設置是否可以進行繪圖緩存
view.setDrawingCacheEnabled(true);
// 如果繪圖緩存無法,強制構建繪圖緩存
view.buildDrawingCache();
// 返回這個緩存視圖
bitmap=view.getDrawingCache();
// 獲取狀態欄高度
Rect frame=new Rect();
// 測量屏幕寬和高
view.getWindowVisibleDisplayFrame(frame);
int stautsHeight = 100;
Log.d("jiangqq", "狀態欄的高度爲:"+stautsHeight);
int width = pActivity.getWindowManager().getDefaultDisplay().getWidth();
int height = (int)width*7/5; //截圖時控制距離,距離屏幕底部多高距離
bitmap = Bitmap.createBitmap(bitmap, 0, stautsHeight, width, height);
Log.d("截圖截圖截圖截圖", "截圖width:"+width+"截圖height:"+height+"頂部開始Y座標"+stautsHeight);
String path = SaveSdCardUtil.saveMyBitmap(System.currentTimeMillis() + ".jpg",bitmap);
return path;
}
//好用的保存
public static String getSavePath(Bitmap bitmap)
{
String path = SaveSdCardUtil.saveMyBitmap(System.currentTimeMillis() + ".jpg",bitmap);
return path;
}
/** 首先默認個文件保存路徑 */
private static final String SAVE_PIC_PATH=Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory().getAbsolutePath() : "/mnt/sdcard";//保存到SD卡
private static final String SAVE_REAL_PATH = SAVE_PIC_PATH+ "/good/savePic";//保存的確切位置
public static void saveFile(Bitmap bm, String fileName, String path) throws IOException {
String fileName1 = System.currentTimeMillis() + ".jpg";
String subForder = SAVE_REAL_PATH + fileName1;
File foder = new File(subForder);
if (!foder.exists()) {
foder.mkdirs();
}
File myCaptureFile = new File(subForder, fileName);
if (!myCaptureFile.exists()) {
myCaptureFile.createNewFile();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
}
public static String createImageFromBitmap(Bitmap bitmap) {
String fileName = System.currentTimeMillis() + ".jpg";
Uri uri;
try {
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +fileName;
File appDir = new File(storePath);
if (!appDir.exists()) {
appDir.mkdir();
}
File file = new File(appDir, fileName);
FileOutputStream fos = new FileOutputStream(file);
//通過io流的方式來壓縮保存圖片
boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 60, fos);
fos.flush();
fos.close();
//把文件插入到系統圖庫
MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
Log.i("fileName", fileName);
return fileName.toString();
}
/**
* 保存圖片到sdcard中
* @param pBitmap
*/
public static boolean savePic(Bitmap pBitmap,String strName)
{
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +strName;
File appDir = new File(storePath);
if (!appDir.exists()) {
appDir.mkdir();
}
FileOutputStream fos=null;
try {
fos=new FileOutputStream(strName);
if(null!=fos)
{
pBitmap.compress(Bitmap.CompressFormat.JPEG, 60, fos);
fos.flush();
fos.close();
return true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return false;
}
//保存文件到指定路徑
public static boolean saveImageToGallery(Context context, Bitmap bmp) {
// 首先保存圖片
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + System.currentTimeMillis()+".jpg";
File appDir = new File(storePath);
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
//通過io流的方式來壓縮保存圖片
boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
fos.flush();
fos.close();
//把文件插入到系統圖庫
MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
//保存圖片後發送廣播通知更新數據庫
Uri uri = Uri.fromFile(file);
// context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
if (isSuccess) {
return true;
} else {
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 獲取一個 View 的緩存視圖
*/
public static Bitmap getCacheBitmapFromView(View v) {
//第一種方案 返回的bitmap不爲空
if (v.getLayoutParams().width<=0||v.getLayoutParams().height<=0){
return null;
}
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
}
存儲爲jpg或者png圖片輔助工具類:
import android.graphics.Bitmap;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by wjj on 2017/9/28.
* @Description 圖片存儲工具類
*/
public class SaveSdCardUtil {
public static String saveMyBitmap(String bitName, Bitmap mBitmap){
String filePath = "";
File f = new File("/sdcard/" + bitName );
filePath = f.getAbsolutePath();
try {
f.createNewFile();
} catch (IOException e) {
Log.e("在保存圖片時出錯","在保存圖片時出錯"+e.toString());
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
public static String saveMyBitmapPng(String bitName, Bitmap mBitmap){
String filePath = "";
File f = new File("/sdcard/" + bitName );
filePath = f.getAbsolutePath();
try {
f.createNewFile();
} catch (IOException e) {
Log.e("在保存圖片時出錯","在保存圖片時出錯"+e.toString());
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
}