Android屏幕截图并保存截取屏幕的图片到指定文件,具体内容如下
注意要添加权限:
1
|
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" /> |
完整代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
package zhangphil.test; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Environment; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import android.view.View; import android.widget.Toast; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class JavaActivity extends AppCompatActivity { @Override protected void onCreate( @Nullable Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.drawable_activity); AppCompatActivity activity = this ; findViewById(R.id.button).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "zhangphil.jpg" ); Bitmap bitmap = screenShot(activity); try { if (!file.exists()) file.createNewFile(); boolean ret = save(bitmap, file, Bitmap.CompressFormat.JPEG, true ); if (ret) { Toast.makeText(getApplicationContext(), "截图已保持至 " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } }); } /** * 保存图片到文件File。 * * @param src 源图片 * @param file 要保存到的文件 * @param format 格式 * @param recycle 是否回收 * @return true 成功 false 失败 */ public static boolean save(Bitmap src, File file, Bitmap.CompressFormat format, boolean recycle) { if (isEmptyBitmap(src)) return false ; OutputStream os; boolean ret = false ; try { os = new BufferedOutputStream( new FileOutputStream(file)); ret = src.compress(format, 100 , os); if (recycle && !src.isRecycled()) src.recycle(); } catch (IOException e) { e.printStackTrace(); } return ret; } /** * 获取当前屏幕截图,不包含状态栏(Status Bar)。 * * @param activity activity * @return Bitmap */ public static Bitmap screenShot(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled( true ); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int statusBarHeight = getStatusBarHeight(activity); int width = ( int ) getDeviceDisplaySize(activity)[ 0 ]; int height = ( int ) getDeviceDisplaySize(activity)[ 1 ]; Bitmap ret = Bitmap.createBitmap(bmp, 0 , statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return ret; } public static float [] getDeviceDisplaySize(Context context) { Resources resources = context.getResources(); DisplayMetrics dm = resources.getDisplayMetrics(); int width = dm.widthPixels; int height = dm.heightPixels; float [] size = new float [ 2 ]; size[ 0 ] = width; size[ 1 ] = height; return size; } public static int getStatusBarHeight(Context context) { int height = 0 ; int resourceId = context.getResources().getIdentifier( "status_bar_height" , "dimen" , "android" ); if (resourceId > 0 ) { height = context.getResources().getDimensionPixelSize(resourceId); } return height; } /** * Bitmap对象是否为空。 */ public static boolean isEmptyBitmap(Bitmap src) { return src == null || src.getWidth() == 0 || src.getHeight() == 0 ; } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/zhangphil/article/details/80741491?utm_source=blogxgwz5