代碼實現
- 添加權限(AndroidManifest.xml文件里)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
/**
* Created by 周旭 on 2017/4/11.
* 截屏工具類
*/
public class ScreenShotUtils {
public static boolean shotScreen(Activity activity) {
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dearxy";
File appDir = new File(storePath);
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
//獲取屏幕截圖
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
try {
FileOutputStream fos = new FileOutputStream(file);
//通過io流的方式來壓縮保存圖片
boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.PNG, 80, fos);
fos.flush();
fos.close();
//保存圖片后發送廣播通知更新數據庫
Uri uri = Uri.fromFile(file);
activity.getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
if (isSuccess) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。