-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppUtils.java
More file actions
181 lines (161 loc) · 5.86 KB
/
AppUtils.java
File metadata and controls
181 lines (161 loc) · 5.86 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
public class AppUtils {
/**
* 关闭键盘
*
* @param activity Activity
*/
public static void hideSoftInput(Activity activity) {
if (activity.getCurrentFocus() != null)
((InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(activity.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 指定小数输出
*
* @param s 输入
* @param format 小数格式,比如保留两位0.00
* @return 输出结果
*/
public static String decimalFormat(String s, String format) {
DecimalFormat decimalFormat = new DecimalFormat(format);
return decimalFormat.format(s);
}
/**
* FilePath To Bitmap
*
* @param context 上下文
* @param filePath 文件路径
*/
public static Bitmap getBitmapFromFilePath(Context context, String filePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
int bmpWidth = options.outWidth;
int bmpHeight = options.outHeight;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
options.inSampleSize = 1;
if (bmpWidth > bmpHeight) {
if (bmpWidth > screenWidth)
options.inSampleSize = bmpWidth / screenWidth;
} else {
if (bmpHeight > screenHeight)
options.inSampleSize = bmpHeight / screenHeight;
}
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
/**
* Bitmap to File
*
* @param bitmap
*/
public static String bitmap2File(Bitmap bitmap) {
File outDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (!outDir.exists()) {
outDir.mkdirs();
}
File outFile = new File(outDir, System.currentTimeMillis() + ".jpg");
try {
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return outFile.getAbsolutePath();
}
/**
* 把Bitmap转Byte
*/
public static byte[] bitmap2Bytes(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
/**
* MD5加密
*
* @param plainText 需要加密的字符串
* @return 加密后字符串
*/
public static String md5(String plainText) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString().toLowerCase();// 32位的加密(转成小写)
buf.toString().substring(8, 24);// 16位的加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
/**
* 安装apk
*
* @param context 上下文
* @param path 文件路劲
*/
public static void installAPK(Context context, String path) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
context.startActivity(intent);
}
/**
* 直接拨号,需要增加CALL_PHONE权限
*
* @param context 上下文
* @param phone 手机号码
*/
public static void actionCall(Context context, String phone) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
intent.setAction(Intent.ACTION_CALL);// 直接拨号
context.startActivity(intent);
}
/**
* 跳到拨号盘-拨打电话
*
* @param context 上下文
* @param phone 手机号码
*/
public static void actionDial(Context context, String phone) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
intent.setAction(Intent.ACTION_DIAL);// 拨号盘
context.startActivity(intent);
}
}