APP工具类
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.net.Uri;
import android.text.TextUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileFilter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.security.auth.x500.X500Principal;
/**
* APP工具类
* APP相关信息工具类。获取版本信息
*/
public final class AppUtils {
private static final boolean DEBUG = true;
private static final String TAG = "AppUtils";
/**
* Don't let anyone instantiate this class.
*/
private AppUtils() {
throw new Error("Do not need instantiate!");
}
/**
* 得到软件版本号
*
* @param context 上下文
* @return 当前版本Code
*/
public static int getVerCode(Context context) {
int verCode = -1;
try {
String packageName = context.getPackageName();
verCode = context.getPackageManager()
.getPackageInfo(packageName, 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return verCode;
}
/**
* 获取应用运行的最大内存
*
* @return 最大内存
*/
public static long getMaxMemory() {
return Runtime.getRuntime().maxMemory() / 1024;
}
/**
* 得到软件显示版本信息
*
* @param context 上下文
* @return 当前版本信息
*/
public static String getVerName(Context context) {
String verName = "";
try {
String packageName = context.getPackageName();
verName = context.getPackageManager()
.getPackageInfo(packageName, 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return verName;
}
/**
* 安装apk
*
* @param context 上下文
* @param file APK文件
*/
public static void installApk(Context context, File file) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
context.startActivity(intent);
}
/**
* 安装apk
*
* @param context 上下文
* @param file APK文件uri
*/
public static void installApk(Context context, Uri file) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(file, "application/vnd.android.package-archive");
context.startActivity(intent);
}
/**
* 卸载apk
*
* @param context 上下文
* @param packageName 包名
*/
public static void uninstallApk(Context context, String packageName) {
Intent intent = new Intent(Intent.ACTION_DELETE);
Uri packageURI = Uri.parse("package:" + packageName);
intent.setData(packageURI);
context.startActivity(intent);
}
/**
* 检测服务是否运行
*
* @param context 上下文
* @param className 类名
* @return 是否运行的状态
*/
public static boolean isServiceRunning(Context context, String className) {
boolean isRunning = false;
ActivityManager activityManager
= (ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> servicesList
= activityManager.getRunningServices(Integer.MAX_VALUE);
for (RunningServiceInfo si : servicesList) {
if (className.equals(si.service.getClassName())) {
isRunning = true;
}
}
return isRunning;
}
/**
* 停止运行服务
*
* @param context 上下文
* @param className 类名
* @return 是否执行成功
*/
public static boolean stopRunningService(Context context, String className) {
Intent intent_service = null;
boolean ret = false;
try {
intent_service = new Intent(context, Class.forName(className));
} catch (Exception e) {
e.printStackTrace();
}
if (intent_service != null) {
ret = context.stopService(intent_service);
}
return ret;
}
/**
* 得到CPU核心数
*
* @return CPU核心数
*/
public static int getNumCores() {
try {
File dir = new File("/sys/devices/system/cpu/");
File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (Pattern.matches("cpu[0-9]", pathname.getName())) {
return true;
}
return false;
}
});
return files.length;
} catch (Exception e) {
return 1;
}
}
/**
* whether this process is named with processName
*
* @param context 上下文
* @param processName 进程名
* @return 是否含有当前的进程
*/
public static boolean isNamedProcess(Context context, String processName) {
if (context == null || TextUtils.isEmpty(processName)) {
return false;
}
int pid = android.os.Process.myPid();
ActivityManager manager = (ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processInfoList
= manager.getRunningAppProcesses();
if (processInfoList == null) {
return true;
}
for (RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
if (processInfo.pid == pid &&
processName.equalsIgnoreCase(processInfo.processName)) {
return true;
}
}
return false;
}
/**
* whether application is in background
* <ul>
* <li>need use permission android.permission.GET_TASKS in Manifest.xml</li>
* </ul>
*
* @param context 上下文
* @return if application is in background return true, otherwise return
* false
*/
public static boolean isApplicationInBackground(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskList = am.getRunningTasks(1);
if (taskList != null && !taskList.isEmpty()) {
ComponentName topActivity = taskList.get(0).topActivity;
if (topActivity != null && !topActivity.getPackageName()
.equals(context.getPackageName())) {
return true;
}
}
return false;
}
/**
* 获取应用签名
*
* @param context 上下文
* @param pkgName 包名
* @return 返回应用的签名
*/
public static String getSign(Context context, String pkgName) {
try {
PackageInfo pis = context.getPackageManager()
.getPackageInfo(pkgName,
PackageManager.GET_SIGNATURES);
return hexdigest(pis.signatures[0].toByteArray());
} catch (NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* 将签名字符串转换成需要的32位签名
*
* @param paramArrayOfByte 签名byte数组
* @return 32位签名字符串
*/
private static String hexdigest(byte[] paramArrayOfByte) {
final char[] hexDigits = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97,
98, 99, 100, 101, 102};
try {
MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
localMessageDigest.update(paramArrayOfByte);
byte[] arrayOfByte = localMessageDigest.digest();
char[] arrayOfChar = new char[32];
for (int i = 0, j = 0; ; i++, j++) {
if (i >= 16) {
return new String(arrayOfChar);
}
int k = arrayOfByte[i];
arrayOfChar[j] = hexDigits[(0xF & k >>> 4)];
arrayOfChar[++j] = hexDigits[(k & 0xF)];
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 清理后台进程与服务
*
* @param context 应用上下文对象context
* @return 被清理的数量
*/
public static int gc(Context context) {
long i = getDeviceUsableMemory(context);
int count = 0; // 清理掉的进程数
ActivityManager am = (ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE);
// 获取正在运行的service列表
List<RunningServiceInfo> serviceList = am.getRunningServices(100);
if (serviceList != null) {
for (RunningServiceInfo service : serviceList) {
if (service.pid == android.os.Process.myPid()) continue;
try {
android.os.Process.killProcess(service.pid);
count++;
} catch (Exception e) {
e.getStackTrace();
}
}
}
// 获取正在运行的进程列表
List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
if (processList != null) {
for (RunningAppProcessInfo process : processList) {
// 一般数值大于RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了
// 一般数值大于RunningAppProcessInfo.IMPORTANCE_VISIBLE的进程都是非可见进程,也就是在后台运行着
if (process.importance >
RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
// pkgList 得到该进程下运行的包名
String[] pkgList = process.pkgList;
for (String pkgName : pkgList) {
if (DEBUG) {
}
try {
am.killBackgroundProcesses(pkgName);
count++;
} catch (Exception e) { // 防止意外发生
e.getStackTrace();
}
}
}
}
}
if (DEBUG) {
}
return count;
}
/**
* 获取设备的可用内存大小
*
* @param context 应用上下文对象context
* @return 当前内存大小
*/
public static int getDeviceUsableMemory(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
// 返回当前系统的可用内存
return (int) (mi.availMem / (1024 * 1024));
}
/**
* 获取系统中所有的应用
*
* @param context 上下文
* @return 应用信息List
*/
public static List<PackageInfo> getAllApps(Context context) {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
List<PackageInfo> paklist = pManager.getInstalledPackages(0);
for (int i = 0; i < paklist.size(); i++) {
PackageInfo pak = paklist.get(i);
if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <=
0) {
// customs applications
apps.add(pak);
}
}
return apps;
}
/**
* 获取手机系统SDK版本
*
* @return 如API 17 则返回 17
*/
public static int getSDKVersion() {
return android.os.Build.VERSION.SDK_INT;
}
/**
* 是否Dalvik模式
*
* @return 结果
*/
public static boolean isDalvik() {
return "Dalvik".equals(getCurrentRuntimeValue());
}
/**
* 是否ART模式
*
* @return 结果
*/
public static boolean isART() {
String currentRuntime = getCurrentRuntimeValue();
return "ART".equals(currentRuntime) ||
"ART debug build".equals(currentRuntime);
}
/**
* 获取手机当前的Runtime
*
* @return 正常情况下可能取值Dalvik, ART, ART debug build;
*/
public static String getCurrentRuntimeValue() {
try {
Class<?> systemProperties = Class.forName(
"android.os.SystemProperties");
try {
Method get = systemProperties.getMethod("get", String.class,
String.class);
if (get == null) {
return "WTF?!";
}
try {
final String value = (String) get.invoke(systemProperties,
"persist.sys.dalvik.vm.lib",
/* Assuming default is */"Dalvik");
if ("libdvm.so".equals(value)) {
return "Dalvik";
} else if ("libart.so".equals(value)) {
return "ART";
} else if ("libartd.so".equals(value)) {
return "ART debug build";
}
return value;
} catch (IllegalAccessException e) {
return "IllegalAccessException";
} catch (IllegalArgumentException e) {
return "IllegalArgumentException";
} catch (InvocationTargetException e) {
return "InvocationTargetException";
}
} catch (NoSuchMethodException e) {
return "SystemProperties.get(String key, String def) method is not found";
}
} catch (ClassNotFoundException e) {
return "SystemProperties class is not found";
}
}
private final static X500Principal DEBUG_DN = new X500Principal("CN=Android Debug,O=Android,C=US");
/**
* 检测当前应用是否是Debug版本
*
* @param ctx 上下文
* @return 是否是Debug版本
*/
public static boolean isDebuggable(Context ctx) {
boolean debuggable = false;
try {
PackageInfo pinfo = ctx.getPackageManager()
.getPackageInfo(ctx.getPackageName(),
PackageManager.GET_SIGNATURES);
Signature signatures[] = pinfo.signatures;
for (int i = 0; i < signatures.length; i++) {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
ByteArrayInputStream stream = new ByteArrayInputStream(
signatures[i].toByteArray());
X509Certificate cert = (X509Certificate) cf.generateCertificate(
stream);
debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
if (debuggable) break;
}
} catch (NameNotFoundException e) {
} catch (CertificateException e) {
}
return debuggable;
}
/**
* 比较版本号的大小,前者大则返回一个正数,后者大返回一个负数,相等则返回0 支持4.1.2,4.1.23.4.1.rc111这种形式
*
* @param version1
* @param version2
* @return
*/
public static int compareVersion(String version1, String version2) throws Exception {
if (version1 == null || version2 == null) {
throw new Exception("compareVersion error:illegal params.");
}
String[] versionArray1 = version1.split("\\.");//注意此处为正则匹配,不能用".";
String[] versionArray2 = version2.split("\\.");
int idx = 0;
int minLength = Math.min(versionArray1.length, versionArray2.length);//取最小长度值
int diff = 0;
while (idx < minLength
&& (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0//先比较长度
&& (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {//再比较字符
++idx;
}
//如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大;
diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
return diff;
}
}
加密
import java.util.Arrays;
/**
* 加密
*/
public class Base64 {
private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
private static final int[] IA = new int[256];
static {
Arrays.fill(IA, -1);
for (int i = 0, iS = CA.length; i < iS; i++)
IA[CA[i]] = i;
IA['='] = 0;
}
private static final byte[] encodingTable = {(byte) 'A', (byte) 'B',
(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G',
(byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',
(byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q',
(byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V',
(byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a',
(byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
(byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k',
(byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p',
(byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u',
(byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',
(byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4',
(byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9',
(byte) '+', (byte) '/'};
private static final byte[] decodingTable;
static {
decodingTable = new byte[128];
for (int i = 0; i < 128; i++) {
decodingTable[i] = (byte) -1;
}
for (int i = 'A'; i <= 'Z'; i++) {
decodingTable[i] = (byte) (i - 'A');
}
for (int i = 'a'; i <= 'z'; i++) {
decodingTable[i] = (byte) (i - 'a' + 26);
}
for (int i = '0'; i <= '9'; i++) {
decodingTable[i] = (byte) (i - '0' + 52);
}
decodingTable['+'] = 62;
decodingTable['/'] = 63;
}
public static byte[] encode(byte[] data) {
byte[] bytes;
int modulus = data.length % 3;
if (modulus == 0) {
bytes = new byte[(4 * data.length) / 3];
} else {
bytes = new byte[4 * ((data.length / 3) + 1)];
}
int dataLength = (data.length - modulus);
int a1;
int a2;
int a3;
for (int i = 0, j = 0; i < dataLength; i += 3, j += 4) {
a1 = data[i] & 0xff;
a2 = data[i + 1] & 0xff;
a3 = data[i + 2] & 0xff;
bytes[j] = encodingTable[(a1 >>> 2) & 0x3f];
bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];
bytes[j + 3] = encodingTable[a3 & 0x3f];
}
int b1;
int b2;
int b3;
int d1;
int d2;
switch (modulus) {
case 0: /* nothing left to do */
break;
case 1:
d1 = data[data.length - 1] & 0xff;
b1 = (d1 >>> 2) & 0x3f;
b2 = (d1 << 4) & 0x3f;
bytes[bytes.length - 4] = encodingTable[b1];
bytes[bytes.length - 3] = encodingTable[b2];
bytes[bytes.length - 2] = (byte) '=';
bytes[bytes.length - 1] = (byte) '=';
break;
case 2:
d1 = data[data.length - 2] & 0xff;
d2 = data[data.length - 1] & 0xff;
b1 = (d1 >>> 2) & 0x3f;
b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
b3 = (d2 << 2) & 0x3f;
bytes[bytes.length - 4] = encodingTable[b1];
bytes[bytes.length - 3] = encodingTable[b2];
bytes[bytes.length - 2] = encodingTable[b3];
bytes[bytes.length - 1] = (byte) '=';
break;
}
return bytes;
}
public static byte[] decode(byte[] data) {
byte[] bytes;
byte b1;
byte b2;
byte b3;
byte b4;
data = discardNonBase64Bytes(data);
if (data[data.length - 2] == '=') {
bytes = new byte[(((data.length / 4) - 1) * 3) + 1];
} else if (data[data.length - 1] == '=') {
bytes = new byte[(((data.length / 4) - 1) * 3) + 2];
} else {
bytes = new byte[((data.length / 4) * 3)];
}
for (int i = 0, j = 0; i < (data.length - 4); i += 4, j += 3) {
b1 = decodingTable[data[i]];
b2 = decodingTable[data[i + 1]];
b3 = decodingTable[data[i + 2]];
b4 = decodingTable[data[i + 3]];
bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
bytes[j + 2] = (byte) ((b3 << 6) | b4);
}
if (data[data.length - 2] == '=') {
b1 = decodingTable[data[data.length - 4]];
b2 = decodingTable[data[data.length - 3]];
bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
} else if (data[data.length - 1] == '=') {
b1 = decodingTable[data[data.length - 4]];
b2 = decodingTable[data[data.length - 3]];
b3 = decodingTable[data[data.length - 2]];
bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
} else {
b1 = decodingTable[data[data.length - 4]];
b2 = decodingTable[data[data.length - 3]];
b3 = decodingTable[data[data.length - 2]];
b4 = decodingTable[data[data.length - 1]];
bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
}
return bytes;
}
public static byte[] decode(String data) {
byte[] bytes;
byte b1;
byte b2;
byte b3;
byte b4;
data = discardNonBase64Chars(data);
if (data.charAt(data.length() - 2) == '=') {
bytes = new byte[(((data.length() / 4) - 1) * 3) + 1];
} else if (data.charAt(data.length() - 1) == '=') {
bytes = new byte[(((data.length() / 4) - 1) * 3) + 2];
} else {
bytes = new byte[((data.length() / 4) * 3)];
}
for (int i = 0, j = 0; i < (data.length() - 4); i += 4, j += 3) {
b1 = decodingTable[data.charAt(i)];
b2 = decodingTable[data.charAt(i + 1)];
b3 = decodingTable[data.charAt(i + 2)];
b4 = decodingTable[data.charAt(i + 3)];
bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
bytes[j + 2] = (byte) ((b3 << 6) | b4);
}
if (data.charAt(data.length() - 2) == '=') {
b1 = decodingTable[data.charAt(data.length() - 4)];
b2 = decodingTable[data.charAt(data.length() - 3)];
bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
} else if (data.charAt(data.length() - 1) == '=') {
b1 = decodingTable[data.charAt(data.length() - 4)];
b2 = decodingTable[data.charAt(data.length() - 3)];
b3 = decodingTable[data.charAt(data.length() - 2)];
bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
} else {
b1 = decodingTable[data.charAt(data.length() - 4)];
b2 = decodingTable[data.charAt(data.length() - 3)];
b3 = decodingTable[data.charAt(data.length() - 2)];
b4 = decodingTable[data.charAt(data.length() - 1)];
bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
}
return bytes;
}
private static byte[] discardNonBase64Bytes(byte[] data) {
byte[] temp = new byte[data.length];
int bytesCopied = 0;
for (int i = 0; i < data.length; i++) {
if (isValidBase64Byte(data[i])) {
temp[bytesCopied++] = data[i];
}
}
byte[] newData = new byte[bytesCopied];
System.arraycopy(temp, 0, newData, 0, bytesCopied);
return newData;
}
private static String discardNonBase64Chars(String data) {
StringBuffer sb = new StringBuffer();
int length = data.length();
for (int i = 0; i < length; i++) {
if (isValidBase64Byte((byte) (data.charAt(i)))) {
sb.append(data.charAt(i));
}
}
return sb.toString();
}
private static boolean isValidBase64Byte(byte b) {
if (b == '=') {
return true;
} else if ((b < 0) || (b >= 128)) {
return false;
} else if (decodingTable[b] == -1) {
return false;
}
return true;
}
/**
* Encodes a raw byte array into a BASE64 <code>String</code> representation i accordance with RFC 2045.
*
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
* little faster.
* @return A BASE64 encoded array. Never <code>null</code>.
*/
public final static String encodeToString(byte[] sArr, boolean lineSep) {
// Reuse char[] since we can't create a String incrementally anyway and StringBuffer/Builder would be slower.
return new String(encodeToChar(sArr, lineSep));
}
/**
* Encodes a raw byte array into a BASE64 <code>char[]</code> representation i accordance with RFC 2045.
*
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
* little faster.
* @return A BASE64 encoded array. Never <code>null</code>.
*/
public final static char[] encodeToChar(byte[] sArr, boolean lineSep) {
// Check special case
int sLen = sArr != null ? sArr.length : 0;
if (sLen == 0)
return new char[0];
int eLen = (sLen / 3) * 3; // Length of even 24-bits.
int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count
int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
char[] dArr = new char[dLen];
// Encode even 24-bits
for (int s = 0, d = 0, cc = 0; s < eLen; ) {
// Copy next three bytes into lower 24 bits of int, paying attension to sign.
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);
// Encode the int into four chars
dArr[d++] = CA[(i >>> 18) & 0x3f];
dArr[d++] = CA[(i >>> 12) & 0x3f];
dArr[d++] = CA[(i >>> 6) & 0x3f];
dArr[d++] = CA[i & 0x3f];
// Add optional line separator
if (lineSep && ++cc == 19 && d < dLen - 2) {
dArr[d++] = '\r';
dArr[d++] = '\n';
cc = 0;
}
}
// Pad and encode last bits if source isn't even 24 bits.
int left = sLen - eLen; // 0 - 2.
if (left > 0) {
// Prepare the int
int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
// Set last four chars
dArr[dLen - 4] = CA[i >> 12];
dArr[dLen - 3] = CA[(i >>> 6) & 0x3f];
dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '=';
dArr[dLen - 1] = '=';
}
return dArr;
}
}
图片工具
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.util.Log;
import android.view.View;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Bitmap工具类主要包括获取Bitmap和对Bitmap的操作
*/
final class BitmapUtil {
private static final boolean DEBUG = false;
private static final String TAG = BitmapUtil.class.getSimpleName();
/**
* Don't let anyone instantiate this class.
*/
private BitmapUtil() {
throw new Error("Do not need instantiate!");
}
/**
* 图片压缩处理(使用Options的方法)
* <p/>
* <br>
* <b>说明</b> 使用方法:
* 首先你要将Options的inJustDecodeBounds属性设置为true,BitmapFactory.decode一次图片 。
* 然后将Options连同期望的宽度和高度一起传递到到本方法中。
* 之后再使用本方法的返回值做参数调用BitmapFactory.decode创建图片。
* <p/>
* <br>
* <b>说明</b> BitmapFactory创建bitmap会尝试为已经构建的bitmap分配内存
* ,这时就会很容易导致OOM出现。为此每一种创建方法都提供了一个可选的Options参数
* ,将这个参数的inJustDecodeBounds属性设置为true就可以让解析方法禁止为bitmap分配内存
* ,返回值也不再是一个Bitmap对象, 而是null。虽然Bitmap是null了,但是Options的outWidth、
* outHeight和outMimeType属性都会被赋值。
*
* @param reqWidth 目标宽度,这里的宽高只是阀值,实际显示的图片将小于等于这个值
* @param reqHeight 目标高度,这里的宽高只是阀值,实际显示的图片将小于等于这个值
*/
public static BitmapFactory.Options calculateInSampleSize(
final BitmapFactory.Options options, final int reqWidth,
final int reqHeight) {
// 源图片的高度和宽度
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > 400 || width > 450) {
if (height > reqHeight || width > reqWidth) {
// 计算出实际宽高和目标宽高的比率
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width
/ (float) reqWidth);
// 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
// 一定都会大于等于目标的宽和高。
inSampleSize = heightRatio < widthRatio ? heightRatio
: widthRatio;
}
}
// 设置压缩比例
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
return options;
}
/**
* 获取一个指定大小的bitmap
*
* @param res Resources
* @param resId 图片ID
* @param reqWidth 目标宽度
* @param reqHeight 目标高度
*/
public static Bitmap getBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// BitmapFactory.Options options = new BitmapFactory.Options();
// options.inJustDecodeBounds = true;
// BitmapFactory.decodeResource(res, resId, options);
// options = BitmapHelper.calculateInSampleSize(options, reqWidth,
// reqHeight);
// return BitmapFactory.decodeResource(res, resId, options);
// 通过JNI的形式读取本地图片达到节省内存的目的
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
options.inPurgeable = true;
options.inInputShareable = true;
InputStream is = res.openRawResource(resId);
return getBitmapFromStream(is, null, reqWidth, reqHeight);
}
/**
* 获取一个指定大小的bitmap
*
* @param reqWidth 目标宽度
* @param reqHeight 目标高度
*/
public static Bitmap getBitmapFromFile(String pathName, int reqWidth,
int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
options = calculateInSampleSize(options, reqWidth, reqHeight);
return BitmapFactory.decodeFile(pathName, options);
}
/**
* 获取一个指定大小的bitmap
*
* @param data Bitmap的byte数组
* @param offset image从byte数组创建的起始位置
* @param length the number of bytes, 从offset处开始的长度
* @param reqWidth 目标宽度
* @param reqHeight 目标高度
*/
public static Bitmap getBitmapFromByteArray(byte[] data, int offset,
int length, int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, offset, length, options);
options = calculateInSampleSize(options, reqWidth, reqHeight);
return BitmapFactory.decodeByteArray(data, offset, length, options);
}
/**
* 把bitmap转化为bytes
*
* @param bitmap 源Bitmap
* @return Byte数组
*/
public static byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
/**
* Stream转换成Byte
*
* @param inputStream InputStream
* @return Byte数组
*/
public static byte[] getBytesFromStream(InputStream inputStream) {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int len;
try {
while ((len = inputStream.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
return os.toByteArray();
}
/**
* 获取一个指定大小的bitmap
*
* @param b Byte数组
* @return 需要的Bitmap
*/
public static Bitmap getBitmapFromBytes(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
/**
* 获取一个指定大小的bitmap
*
* @param is 从输入流中读取Bitmap
* @param outPadding If not null, return the padding rect for the bitmap if it
* exists, otherwise set padding to [-1,-1,-1,-1]. If no bitmap
* is returned (null) then padding is unchanged.
* @param reqWidth 目标宽度
* @param reqHeight 目标高度
*/
public static Bitmap getBitmapFromStream(InputStream is, Rect outPadding,
int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, outPadding, options);
options = calculateInSampleSize(options, reqWidth, reqHeight);
return BitmapFactory.decodeStream(is, outPadding, options);
}
/**
* 从View获取Bitmap
*
* @param view View
* @return Bitmap
*/
public static Bitmap getBitmapFromView(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.layout(view.getLeft(), view.getTop(), view.getRight(),
view.getBottom());
view.draw(canvas);
return bitmap;
}
/**
* 把一个View的对象转换成bitmap
*
* @param view View
* @return Bitmap
*/
public static Bitmap getBitmapFromView2(View view) {
view.clearFocus();
view.setPressed(false);
// 能画缓存就返回false
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
int color = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheBackgroundColor(0);
if (color != 0) {
view.destroyDrawingCache();
}
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) {
if (DEBUG) {
Log.e(TAG, "failed getViewBitmap(" + view + ")",
new RuntimeException());
}
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCache);
view.setDrawingCacheBackgroundColor(color);
return bitmap;
}
/**
* 将Drawable转化为Bitmap
*
* @param drawable Drawable
* @return Bitmap
*/
public static Bitmap getBitmapFromDrawable(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888
: Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return bitmap;
}
/**
* 合并Bitmap
*
* @param bgd 背景Bitmap
* @param fg 前景Bitmap
* @return 合成后的Bitmap
*/
public static Bitmap combineImages(Bitmap bgd, Bitmap fg) {
Bitmap bmp;
int width = bgd.getWidth() > fg.getWidth() ? bgd.getWidth() : fg
.getWidth();
int height = bgd.getHeight() > fg.getHeight() ? bgd.getHeight() : fg
.getHeight();
bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(bgd, 0, 0, null);
canvas.drawBitmap(fg, 0, 0, paint);
return bmp;
}
/**
* 合并
*
* @param bgd 后景Bitmap
* @param fg 前景Bitmap
* @return 合成后Bitmap
*/
public static Bitmap combineImagesToSameSize(Bitmap bgd, Bitmap fg) {
Bitmap bmp;
int width = bgd.getWidth() < fg.getWidth() ? bgd.getWidth() : fg
.getWidth();
int height = bgd.getHeight() < fg.getHeight() ? bgd.getHeight() : fg
.getHeight();
if (fg.getWidth() != width && fg.getHeight() != height) {
fg = zoom(fg, width, height);
}
if (bgd.getWidth() != width && bgd.getHeight() != height) {
bgd = zoom(bgd, width, height);
}
bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(bgd, 0, 0, null);
canvas.drawBitmap(fg, 0, 0, paint);
return bmp;
}
/**
* 放大缩小图片
*
* @param bitmap 源Bitmap
* @param w 宽
* @param h 高
* @return 目标Bitmap
*/
public static Bitmap zoom(Bitmap bitmap, int w, int h) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float) w / width);
float scaleHeight = ((float) h / height);
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
matrix, true);
return newbmp;
}
/**
* 获得圆角图片的方法
*
* @param bitmap 源Bitmap
* @param roundPx 圆角大小
* @return 期望Bitmap
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
/**
* 获得带倒影的图片方法
*
* @param bitmap 源Bitmap
* @return 带倒影的Bitmap
*/
public static Bitmap createReflectionBitmap(Bitmap bitmap) {
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
width, height / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
/**
* 压缩图片大小
*
* @param image 源Bitmap
* @return 压缩后的Bitmap
*/
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset();// 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
options -= 10;// 每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
return bitmap;
}
/**
* 将彩色图转换为灰度图
*
* @param img 源Bitmap
* @return 返回转换好的位图
*/
public static Bitmap convertGreyImg(Bitmap img) {
int width = img.getWidth(); // 获取位图的宽
int height = img.getHeight(); // 获取位图的高
int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
img.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
grey = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565);
result.setPixels(pixels, 0, width, 0, 0, width, height);
return result;
}
/**
* 转换图片成圆形
*
* @param bitmap 传入Bitmap对象
* @return 圆形Bitmap
*/
public static Bitmap getRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
}
/**
* Returns a Bitmap representing the thumbnail of the specified Bitmap. The
* size of the thumbnail is defined by the dimension
* android.R.dimen.launcher_application_icon_size.
* <p/>
* This method is not thread-safe and should be invoked on the UI thread
* only.
*
* @param bitmap The bitmap to get a thumbnail of.
* @param context The application's context.
* @return A thumbnail for the specified bitmap or the bitmap itself if the
* thumbnail could not be created.
*/
public static Bitmap createThumbnailBitmap(Bitmap bitmap, Context context) {
int sIconWidth = -1;
int sIconHeight = -1;
final Resources resources = context.getResources();
sIconWidth = sIconHeight = (int) resources
.getDimension(android.R.dimen.app_icon_size);
final Paint sPaint = new Paint();
final Rect sBounds = new Rect();
final Rect sOldBounds = new Rect();
Canvas sCanvas = new Canvas();
int width = sIconWidth;
int height = sIconHeight;
sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
Paint.FILTER_BITMAP_FLAG));
final int bitmapWidth = bitmap.getWidth();
final int bitmapHeight = bitmap.getHeight();
if (width > 0 && height > 0) {
if (width < bitmapWidth || height < bitmapHeight) {
final float ratio = (float) bitmapWidth / bitmapHeight;
if (bitmapWidth > bitmapHeight) {
height = (int) (width / ratio);
} else if (bitmapHeight > bitmapWidth) {
width = (int) (height * ratio);
}
final Config c = (width == sIconWidth && height == sIconHeight) ? bitmap
.getConfig() : Config.ARGB_8888;
final Bitmap thumb = Bitmap.createBitmap(sIconWidth,
sIconHeight, c);
final Canvas canvas = sCanvas;
final Paint paint = sPaint;
canvas.setBitmap(thumb);
paint.setDither(false);
paint.setFilterBitmap(true);
sBounds.set((sIconWidth - width) / 2,
(sIconHeight - height) / 2, width, height);
sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
return thumb;
} else if (bitmapWidth < width || bitmapHeight < height) {
final Config c = Config.ARGB_8888;
final Bitmap thumb = Bitmap.createBitmap(sIconWidth,
sIconHeight, c);
final Canvas canvas = sCanvas;
final Paint paint = sPaint;
canvas.setBitmap(thumb);
paint.setDither(false);
paint.setFilterBitmap(true);
canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2,
(sIconHeight - bitmapHeight) / 2, paint);
return thumb;
}
}
return bitmap;
}
/**
* 生成水印图片 水印在右下角
*
* @param src the bitmap object you want proecss
* @param watermark the water mark above the src
* @return return a bitmap object ,if paramter's length is 0,return null
*/
public static Bitmap createWatermarkBitmap(Bitmap src, Bitmap watermark) {
if (src == null) {
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
// create the new blank bitmap
Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas(newb);
// draw src into
cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src
// draw watermark into
cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印
// save all clip
cv.save(Canvas.ALL_SAVE_FLAG);// 保存
// store
cv.restore();// 存储
return newb;
}
/**
* 重新编码Bitmap
*
* @param src 需要重新编码的Bitmap
* @param format 编码后的格式(目前只支持png和jpeg这两种格式)
* @param quality 重新生成后的bitmap的质量
* @return 返回重新生成后的bitmap
*/
public static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
int quality) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
src.compress(format, quality, os);
byte[] array = os.toByteArray();
return BitmapFactory.decodeByteArray(array, 0, array.length);
}
/**
* 图片压缩方法:(使用compress的方法)
* <p/>
* <br>
* <b>说明</b> 如果bitmap本身的大小小于maxSize,则不作处理
*
* @param bitmap 要压缩的图片
* @param maxSize 压缩后的大小,单位kb
*/
public static void compress(Bitmap bitmap, double maxSize) {
// 将bitmap放至数组中,意在获得bitmap的大小(与实际读取的原文件要大)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 格式、质量、输出流
bitmap.compress(Bitmap.CompressFormat.PNG, 70, baos);
byte[] b = baos.toByteArray();
// 将字节换成KB
double mid = b.length / 1024;
// 获取bitmap大小 是允许最大大小的多少倍
double i = mid / maxSize;
// 判断bitmap占用空间是否大于允许最大空间 如果大于则压缩 小于则不压缩
if (i > 1) {
// 缩放图片 此处用到平方根 将宽带和高度压缩掉对应的平方根倍
// (保持宽高不变,缩放后也达到了最大占用空间的大小)
bitmap = scale(bitmap, bitmap.getWidth() / Math.sqrt(i),
bitmap.getHeight() / Math.sqrt(i));
}
}
/**
* 图片的缩放方法
*
* @param src :源图片资源
* @param newWidth :缩放后宽度
* @param newHeight :缩放后高度
*/
public static Bitmap scale(Bitmap src, double newWidth, double newHeight) {
// 记录src的宽高
float width = src.getWidth();
float height = src.getHeight();
// 创建一个matrix容器
Matrix matrix = new Matrix();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 开始缩放
matrix.postScale(scaleWidth, scaleHeight);
// 创建缩放后的图片
return Bitmap.createBitmap(src, 0, 0, (int) width, (int) height,
matrix, true);
}
/**
* 图片的缩放方法
*
* @param src :源图片资源
* @param scaleMatrix :缩放规则
*/
public static Bitmap scale(Bitmap src, Matrix scaleMatrix) {
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(),
scaleMatrix, true);
}
/**
* 图片的缩放方法
*
* @param src :源图片资源
* @param scaleX :横向缩放比例
* @param scaleY :纵向缩放比例
*/
public static Bitmap scale(Bitmap src, float scaleX, float scaleY) {
Matrix matrix = new Matrix();
matrix.postScale(scaleX, scaleY);
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(),
matrix, true);
}
/**
* 图片的缩放方法
*
* @param src :源图片资源
* @param scale :缩放比例
*/
public static Bitmap scale(Bitmap src, float scale) {
return scale(src, scale, scale);
}
/**
* 旋转图片
*
* @param angle 旋转角度
* @param bitmap 要旋转的图片
* @return 旋转后的图片
*/
public static Bitmap rotate(Bitmap bitmap, int angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
}
/**
* 水平翻转处理
*
* @param bitmap 原图
* @return 水平翻转后的图片
*/
public static Bitmap reverseByHorizontal(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.preScale(-1, 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, false);
}
/**
* 垂直翻转处理
*
* @param bitmap 原图
* @return 垂直翻转后的图片
*/
public static Bitmap reverseByVertical(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, false);
}
/**
* 更改图片色系,变亮或变暗
*
* @param delta 图片的亮暗程度值,越小图片会越亮,取值范围(0,24)
* @return
*/
public static Bitmap adjustTone(Bitmap src, int delta) {
if (delta >= 24 || delta <= 0) {
return null;
}
// 设置高斯矩阵
int[] gauss = new int[]{1, 2, 1, 2, 4, 2, 1, 2, 1};
int width = src.getWidth();
int height = src.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
Config.RGB_565);
int pixR = 0;
int pixG = 0;
int pixB = 0;
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int idx = 0;
int[] pixels = new int[width * height];
src.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
idx = 0;
for (int m = -1; m <= 1; m++) {
for (int n = -1; n <= 1; n++) {
pixColor = pixels[(i + m) * width + k + n];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR += (pixR * gauss[idx]);
newG += (pixG * gauss[idx]);
newB += (pixB * gauss[idx]);
idx++;
}
}
newR /= delta;
newG /= delta;
newB /= delta;
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
pixels[i * width + k] = Color.argb(255, newR, newG, newB);
newR = 0;
newG = 0;
newB = 0;
}
}
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 将彩色图转换为黑白图
*
* @param bmp 位图
* @return 返回转换好的位图
*/
public static Bitmap convertToBlackWhite(Bitmap bmp) {
int width = bmp.getWidth();
int height = bmp.getHeight();
int[] pixels = new int[width * height];
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24; // 默认将bitmap当成24色图片
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
return newBmp;
}
/**
* 读取图片属性:图片被旋转的角度
*
* @param path 图片绝对路径
* @return 旋转的角度
*/
public static int getImageDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
/**
* Apply a blur to a Bitmap
*
* @param context Application context
* @param sentBitmap Bitmap to be converted
* @param radius Desired Radius, 0 < r < 25
* @return a copy of the image with a blur
*/
@SuppressLint("InlinedApi")
public static Bitmap blur(Context context, Bitmap sentBitmap, int radius) {
if (radius < 0) {
radius = 0;
if (DEBUG) {
}
} else if (radius > 25) {
radius = 25;
if (DEBUG) {
}
}
if (Build.VERSION.SDK_INT > 16) {
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs,
sentBitmap, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs,
input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs,
Element.U8_4(rs));
script.setRadius(radius /* e.g. 3.f */);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return bitmap;
}
// Stack Blur v1.0 from
// http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
//
// Java Author: Mario Klingemann <mario at quasimondo.com>
// http://incubator.quasimondo.com
// created Feburary 29, 2004
// Android port : Yahel Bouaziz <yahel at kayenko.com>
// http://www.kayenko.com
// ported april 5th, 2012
// This is a compromise between Gaussian Blur and Box blur
// It creates much better looking blurs than Box Blur, but is
// 7x faster than my Gaussian Blur implementation.
//
// I called it Stack Blur because this describes best how this
// filter works internally: it creates a kind of moving stack
// of colors whilst scanning through the image. Thereby it
// just has to add one new block of color to the right side
// of the stack and remove the leftmost color. The remaining
// colors on the topmost layer of the stack are either added on
// or reduced by one, depending on if they are on the right or
// on the left side of the stack.
//
// If you are using this algorithm in your code please add
// the following line:
//
// Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com>
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
if (radius < 1) {
return (null);
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int[] pix = new int[w * h];
Log.e("pix", w + " " + h + " " + pix.length);
bitmap.getPixels(pix, 0, w, 0, 0, w, h);
int wm = w - 1;
int hm = h - 1;
int wh = w * h;
int div = radius + radius + 1;
int r[] = new int[wh];
int g[] = new int[wh];
int b[] = new int[wh];
int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
int vmin[] = new int[Math.max(w, h)];
int divsum = (div + 1) >> 1;
divsum *= divsum;
int dv[] = new int[256 * divsum];
for (i = 0; i < 256 * divsum; i++) {
dv[i] = (i / divsum);
}
yw = yi = 0;
int[][] stack = new int[div][3];
int stackpointer;
int stackstart;
int[] sir;
int rbs;
int r1 = radius + 1;
int routsum, goutsum, boutsum;
int rinsum, ginsum, binsum;
for (y = 0; y < h; y++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
for (i = -radius; i <= radius; i++) {
p = pix[yi + Math.min(wm, Math.max(i, 0))];
sir = stack[i + radius];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rbs = r1 - Math.abs(i);
rsum += sir[0] * rbs;
gsum += sir[1] * rbs;
bsum += sir[2] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
}
stackpointer = radius;
for (x = 0; x < w; x++) {
r[yi] = dv[rsum];
g[yi] = dv[gsum];
b[yi] = dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - radius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (y == 0) {
vmin[x] = Math.min(x + radius + 1, wm);
}
p = pix[yw + vmin[x]];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[(stackpointer) % div];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi++;
}
yw += w;
}
for (x = 0; x < w; x++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
yp = -radius * w;
for (i = -radius; i <= radius; i++) {
yi = Math.max(0, yp) + x;
sir = stack[i + radius];
sir[0] = r[yi];
sir[1] = g[yi];
sir[2] = b[yi];
rbs = r1 - Math.abs(i);
rsum += r[yi] * rbs;
gsum += g[yi] * rbs;
bsum += b[yi] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
if (i < hm) {
yp += w;
}
}
yi = x;
stackpointer = radius;
for (y = 0; y < h; y++) {
// Preserve alpha channel: ( 0xff000000 & pix[yi] )
pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16)
| (dv[gsum] << 8) | dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - radius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (x == 0) {
vmin[y] = Math.min(y + r1, hm) * w;
}
p = x + vmin[y];
sir[0] = r[p];
sir[1] = g[p];
sir[2] = b[p];
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[stackpointer];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi += w;
}
}
Log.e("pix", w + " " + h + " " + pix.length);
bitmap.setPixels(pix, 0, w, 0, 0, w, h);
return (bitmap);
}
/**
* 饱和度处理
*
* @param bitmap 原图
* @param saturationValue 新的饱和度值
* @return 改变了饱和度值之后的图片
*/
public static Bitmap saturation(Bitmap bitmap, int saturationValue) {
// 计算出符合要求的饱和度值
float newSaturationValue = saturationValue * 1.0F / 127;
// 创建一个颜色矩阵
ColorMatrix saturationColorMatrix = new ColorMatrix();
// 设置饱和度值
saturationColorMatrix.setSaturation(newSaturationValue);
// 创建一个画笔并设置其颜色过滤器
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(saturationColorMatrix));
// 创建一个新的图片并创建画布
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
// 将原图使用给定的画笔画到画布上
canvas.drawBitmap(bitmap, 0, 0, paint);
return newBitmap;
}
/**
* 亮度处理
*
* @param bitmap 原图
* @param lumValue 新的亮度值
* @return 改变了亮度值之后的图片
*/
public static Bitmap lum(Bitmap bitmap, int lumValue) {
// 计算出符合要求的亮度值
float newlumValue = lumValue * 1.0F / 127;
// 创建一个颜色矩阵
ColorMatrix lumColorMatrix = new ColorMatrix();
// 设置亮度值
lumColorMatrix.setScale(newlumValue, newlumValue, newlumValue, 1);
// 创建一个画笔并设置其颜色过滤器
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(lumColorMatrix));
// 创建一个新的图片并创建画布
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
// 将原图使用给定的画笔画到画布上
canvas.drawBitmap(bitmap, 0, 0, paint);
return newBitmap;
}
/**
* 色相处理
*
* @param bitmap 原图
* @param hueValue 新的色相值
* @return 改变了色相值之后的图片
*/
public static Bitmap hue(Bitmap bitmap, int hueValue) {
// 计算出符合要求的色相值
float newHueValue = (hueValue - 127) * 1.0F / 127 * 180;
// 创建一个颜色矩阵
ColorMatrix hueColorMatrix = new ColorMatrix();
// 控制让红色区在色轮上旋转的角度
hueColorMatrix.setRotate(0, newHueValue);
// 控制让绿红色区在色轮上旋转的角度
hueColorMatrix.setRotate(1, newHueValue);
// 控制让蓝色区在色轮上旋转的角度
hueColorMatrix.setRotate(2, newHueValue);
// 创建一个画笔并设置其颜色过滤器
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(hueColorMatrix));
// 创建一个新的图片并创建画布
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
// 将原图使用给定的画笔画到画布上
canvas.drawBitmap(bitmap, 0, 0, paint);
return newBitmap;
}
/**
* 亮度、色相、饱和度处理
*
* @param bitmap 原图
* @param lumValue 亮度值
* @param hueValue 色相值
* @param saturationValue 饱和度值
* @return 亮度、色相、饱和度处理后的图片
*/
public static Bitmap lumAndHueAndSaturation(Bitmap bitmap, int lumValue,
int hueValue, int saturationValue) {
// 计算出符合要求的饱和度值
float newSaturationValue = saturationValue * 1.0F / 127;
// 计算出符合要求的亮度值
float newlumValue = lumValue * 1.0F / 127;
// 计算出符合要求的色相值
float newHueValue = (hueValue - 127) * 1.0F / 127 * 180;
// 创建一个颜色矩阵并设置其饱和度
ColorMatrix colorMatrix = new ColorMatrix();
// 设置饱和度值
colorMatrix.setSaturation(newSaturationValue);
// 设置亮度值
colorMatrix.setScale(newlumValue, newlumValue, newlumValue, 1);
// 控制让红色区在色轮上旋转的角度
colorMatrix.setRotate(0, newHueValue);
// 控制让绿红色区在色轮上旋转的角度
colorMatrix.setRotate(1, newHueValue);
// 控制让蓝色区在色轮上旋转的角度
colorMatrix.setRotate(2, newHueValue);
// 创建一个画笔并设置其颜色过滤器
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
// 创建一个新的图片并创建画布
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
// 将原图使用给定的画笔画到画布上
canvas.drawBitmap(bitmap, 0, 0, paint);
return newBitmap;
}
/**
* 怀旧效果处理
*
* @param bitmap 原图
* @return 怀旧效果处理后的图片
*/
public static Bitmap nostalgic(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap newBitmap = Bitmap.createBitmap(width, height,
Config.RGB_565);
int pixColor = 0;
int pixR = 0;
int pixG = 0;
int pixB = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 0; i < height; i++) {
for (int k = 0; k < width; k++) {
pixColor = pixels[width * i + k];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB);
newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB);
newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB);
int newColor = Color.argb(255, newR > 255 ? 255 : newR,
newG > 255 ? 255 : newG, newB > 255 ? 255 : newB);
pixels[width * i + k] = newColor;
}
}
newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return newBitmap;
}
/**
* 柔化效果处理
*
* @param bitmap 原图
* @return 柔化效果处理后的图片
*/
public static Bitmap soften(Bitmap bitmap) {
// 高斯矩阵
int[] gauss = new int[]{1, 2, 1, 2, 4, 2, 1, 2, 1};
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap newBitmap = Bitmap.createBitmap(width, height,
Config.RGB_565);
int pixR = 0;
int pixG = 0;
int pixB = 0;
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int delta = 16; // 值越小图片会越亮,越大则越暗
int idx = 0;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
idx = 0;
for (int m = -1; m <= 1; m++) {
for (int n = -1; n <= 1; n++) {
pixColor = pixels[(i + m) * width + k + n];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = newR + (int) (pixR * gauss[idx]);
newG = newG + (int) (pixG * gauss[idx]);
newB = newB + (int) (pixB * gauss[idx]);
idx++;
}
}
newR /= delta;
newG /= delta;
newB /= delta;
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
pixels[i * width + k] = Color.argb(255, newR, newG, newB);
newR = 0;
newG = 0;
newB = 0;
}
}
newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return newBitmap;
}
/**
* 光照效果处理
*
* @param bitmap 原图
* @param centerX 光源在X轴的位置
* @param centerY 光源在Y轴的位置
* @return 光照效果处理后的图片
*/
public static Bitmap sunshine(Bitmap bitmap, int centerX, int centerY) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
Bitmap newBitmap = Bitmap.createBitmap(width, height,
Config.RGB_565);
int pixR = 0;
int pixG = 0;
int pixB = 0;
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int radius = Math.min(centerX, centerY);
final float strength = 150F; // 光照强度 100~150
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
int pos = 0;
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
pos = i * width + k;
pixColor = pixels[pos];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = pixR;
newG = pixG;
newB = pixB;
// 计算当前点到光照中心的距离,平面座标系中求两点之间的距离
int distance = (int) (Math.pow((centerY - i), 2) + Math.pow(
centerX - k, 2));
if (distance < radius * radius) {
// 按照距离大小计算增加的光照值
int result = (int) (strength * (1.0 - Math.sqrt(distance)
/ radius));
newR = pixR + result;
newG = pixG + result;
newB = pixB + result;
}
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
pixels[pos] = Color.argb(255, newR, newG, newB);
}
}
newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return newBitmap;
}
/**
* 底片效果处理
*
* @param bitmap 原图
* @return 底片效果处理后的图片
*/
public static Bitmap film(Bitmap bitmap) {
// RGBA的最大值
final int MAX_VALUE = 255;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap newBitmap = Bitmap.createBitmap(width, height,
Config.RGB_565);
int pixR = 0;
int pixG = 0;
int pixB = 0;
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
int pos = 0;
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
pos = i * width + k;
pixColor = pixels[pos];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = MAX_VALUE - pixR;
newG = MAX_VALUE - pixG;
newB = MAX_VALUE - pixB;
newR = Math.min(MAX_VALUE, Math.max(0, newR));
newG = Math.min(MAX_VALUE, Math.max(0, newG));
newB = Math.min(MAX_VALUE, Math.max(0, newB));
pixels[pos] = Color.argb(MAX_VALUE, newR, newG, newB);
}
}
newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return newBitmap;
}
/**
* 锐化效果处理
*
* @param bitmap 原图
* @return 锐化效果处理后的图片
*/
public static Bitmap sharpen(Bitmap bitmap) {
// 拉普拉斯矩阵
int[] laplacian = new int[]{-1, -1, -1, -1, 9, -1, -1, -1, -1};
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap newBitmap = Bitmap.createBitmap(width, height,
Config.RGB_565);
int pixR = 0;
int pixG = 0;
int pixB = 0;
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int idx = 0;
float alpha = 0.3F;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
idx = 0;
for (int m = -1; m <= 1; m++) {
for (int n = -1; n <= 1; n++) {
pixColor = pixels[(i + n) * width + k + m];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = newR + (int) (pixR * laplacian[idx] * alpha);
newG = newG + (int) (pixG * laplacian[idx] * alpha);
newB = newB + (int) (pixB * laplacian[idx] * alpha);
idx++;
}
}
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
pixels[i * width + k] = Color.argb(255, newR, newG, newB);
newR = 0;
newG = 0;
newB = 0;
}
}
newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return newBitmap;
}
/**
* 浮雕效果处理
*
* @param bitmap 原图
* @return 浮雕效果处理后的图片
*/
public static Bitmap emboss(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap newBitmap = Bitmap.createBitmap(width, height,
Config.RGB_565);
int pixR = 0;
int pixG = 0;
int pixB = 0;
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
int pos = 0;
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
pos = i * width + k;
pixColor = pixels[pos];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
pixColor = pixels[pos + 1];
newR = Color.red(pixColor) - pixR + 127;
newG = Color.green(pixColor) - pixG + 127;
newB = Color.blue(pixColor) - pixB + 127;
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
pixels[pos] = Color.argb(255, newR, newG, newB);
}
}
newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return newBitmap;
}
/**
* 将YUV格式的图片的源数据从横屏模式转为竖屏模式,注意:将源图片的宽高互换一下就是新图片的宽高
*
* @param sourceData YUV格式的图片的源数据
* @param width 源图片的宽
* @param height 源图片的高
* @return
*/
public static final byte[] yuvLandscapeToPortrait(byte[] sourceData,
int width, int height) {
byte[] rotatedData = new byte[sourceData.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = sourceData[x + y
* width];
}
return rotatedData;
}
}
数据清除管理器
import android.content.Context;
import android.os.Environment;
import java.io.File;
import java.math.BigDecimal;
/**
* 本应用数据清除管理器
* 主要功能有清除内/外缓存,清除数据库,清除sharedPreference,清除files和清除自定义目录
*/
public class DataCleanManager {
/**
* 清除本应用内部缓存(/data/data/com.xxx.xxx/cache)
*
* @param context 上下文
*/
public static void cleanInternalCache(Context context) {
deleteFilesByDirectory(context.getCacheDir());
}
/**
* 清除本应用所有数据库(/data/data/com.xxx.xxx/databases)
*
* @param context 上下文
*/
public static void cleanDatabases(Context context) {
deleteFilesByDirectory(new File(context.getFilesDir().getPath()
+ context.getPackageName() + "/databases"));
}
/**
* 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs)
*
* @param context 上下文
*/
public static void cleanSharedPreference(Context context) {
deleteFilesByDirectory(new File(context.getFilesDir().getPath()
+ context.getPackageName() + "/shared_prefs"));
}
/**
* 按名字清除本应用数据库
*
* @param context 上下文
* @param dbName 数据库名称
*/
public static void cleanDatabaseByName(Context context, String dbName) {
context.deleteDatabase(dbName);
}
/**
* 清除/data/data/com.xxx.xxx/files下的内容
*
* @param context 上下文
*/
public static void cleanFiles(Context context) {
deleteFilesByDirectory(context.getFilesDir());
}
/**
* 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache)
*
* @param context 上下文
*/
public static void cleanExternalCache(Context context) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
deleteFilesByDirectory(context.getExternalCacheDir());
}
}
/**
* 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除
*
* @param filePath 文件路径
*/
public static void cleanCustomCache(String filePath) {
deleteFilesByDirectory(new File(filePath));
}
/**
* 清除本应用所有的数据
*
* @param context 上下文
* @param filePath 文件路径
*/
public static void cleanApplicationData(Context context, String... filePath) {
cleanInternalCache(context);
cleanExternalCache(context);
cleanDatabases(context);
cleanSharedPreference(context);
cleanFiles(context);
for (String fp : filePath) {
cleanCustomCache(fp);
}
}
/**
* 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理
*
* @param directory 文件夹File对象
*/
private static void deleteFilesByDirectory(File directory) {
if (directory != null && directory.exists() && directory.isDirectory()) {
for (File item : directory.listFiles()) {
item.delete();
}
}
}
// 获取文件
//Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
//Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据
public static long getFolderSize(File file) throws Exception {
long size = 0;
try {
File[] fileList = file.listFiles();
for (File aFileList : fileList) {
// 如果下面还有文件
if (aFileList.isDirectory()) {
size = size + getFolderSize(aFileList);
} else {
size = size + aFileList.length();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return size;
}
public static String getCacheSize(File file) throws Exception {
return getFormatSize(getFolderSize(file));
}
/**
*
*
* @param size 传入的大小
* @return 格式化单位返回格式化之后的值
*/
public static String getFormatSize(double size) {
double kiloByte = size / 1024;
if (kiloByte < 1) {
return size + "Byte";
}
double megaByte = kiloByte / 1024;
if (megaByte < 1) {
BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "KB";
}
double gigaByte = megaByte / 1024;
if (gigaByte < 1) {
BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "MB";
}
double teraBytes = gigaByte / 1024;
if (teraBytes < 1) {
BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "GB";
}
BigDecimal result4 = new BigDecimal(teraBytes);
return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
+ "TB";
}
}
日期和时间选择器
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTimePickDialogUtil implements OnDateChangedListener,
OnTimeChangedListener {
private DatePicker datePicker;
private TimePicker timePicker;
private AlertDialog ad;
private String dateTime;
private Date date;
private Activity activity;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public DateTimePickDialogUtil(Activity activity, Date date) {
this.activity = activity;
this.date = date;
}
public void init(DatePicker datePicker, TimePicker timePicker) {
Calendar calendar = Calendar.getInstance();
if (null == date) {
date = new Date();
}
calendar.setTime(date);
datePicker.init(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), this);
timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
}
/**
* 弹出日期时间选择框方法
*/
public AlertDialog dateTimePicKDialog(final Date fDate, final TextView inputDate) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout dateTimeLayout = new LinearLayout(activity);
datePicker = new DatePicker(activity);
timePicker = new TimePicker(activity);
dateTimeLayout.addView(datePicker, params);
dateTimeLayout.addView(timePicker, params);
init(datePicker, timePicker);
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(this);
ad = new AlertDialog.Builder(activity)
.setTitle(sdf.format(date))
.setView(dateTimeLayout)
.setPositiveButton("设置", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
inputDate.setText(dateTime);
fDate.setTime(date.getTime());
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// inputDate.setText("");
}
}).show();
onDateChanged(null, 0, 0, 0);
return ad;
}
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
onDateChanged(null, 0, 0, 0);
}
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// 获得日历实例
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(),
datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
date = calendar.getTime();
dateTime = sdf.format(date);
ad.setTitle(dateTime);
}
}
时间日期工具
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
/**
* 英文简写如:2010
*/
public static String FORMAT_Y = "yyyy";
/**
* 英文简写如:12:01
*/
public static String FORMAT_HM = "HH:mm";
/**
* 英文简写如:1-12 12:01
*/
public static String FORMAT_MDHM = "MM-dd HH:mm";
/**
* 英文简写(默认)如:2010-12-01
*/
public static String FORMAT_YMD = "yyyy-MM-dd";
/**
* 英文全称 如:2010-12-01 23:15
*/
public static String FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
/**
* 英文全称 如:2010-12-01 23:15:06
*/
public static String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
/**
* 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
/**
* 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL_SN = "yyyyMMddHHmmssS";
/**
* 中文简写 如:2010年12月01日
*/
public static String FORMAT_YMD_CN = "yyyy年MM月dd日";
/**
* 中文简写 如:2010年12月01日 12时
*/
public static String FORMAT_YMDH_CN = "yyyy年MM月dd日 HH时";
/**
* 中文简写 如:2010年12月01日 12时12分
*/
public static String FORMAT_YMDHM_CN = "yyyy年MM月dd日 HH时mm分";
/**
* 中文全称 如:2010年12月01日 23时15分06秒
*/
public static String FORMAT_YMDHMS_CN = "yyyy年MM月dd日 HH时mm分ss秒";
/**
* 精确到毫秒的完整中文时间
*/
public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
public static Calendar calendar = null;
private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
public static Date str2Date(String str) {
return str2Date(str, null);
}
public static Date str2Date(String str, String format) {
if (str == null || str.length() == 0) {
return null;
}
if (format == null || format.length() == 0) {
format = FORMAT;
}
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
public static Calendar str2Calendar(String str) {
return str2Calendar(str, null);
}
public static Calendar str2Calendar(String str, String format) {
Date date = str2Date(str, format);
if (date == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
return date2Str(c, null);
}
public static String date2Str(Calendar c, String format) {
if (c == null) {
return null;
}
return date2Str(c.getTime(), format);
}
public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
return date2Str(d, null);
}
public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
if (d == null) {
return null;
}
if (format == null || format.length() == 0) {
format = FORMAT;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
String s = sdf.format(d);
return s;
}
public static String getCurDateStr() {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" +
c.get(Calendar.DAY_OF_MONTH) + "-" +
c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) +
":" + c.get(Calendar.SECOND);
}
/**
* 获得当前日期的字符串格式
*
* @param format 格式化的类型
* @return 返回格式化之后的事件
*/
public static String getCurDateStr(String format) {
Calendar c = Calendar.getInstance();
return date2Str(c, format);
}
/**
* @param time 当前的时间
* @return 格式到秒
*/
public static String getMillon(long time) {
return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);
}
/**
* @param time 当前的时间
* @return 当前的天
*/
public static String getDay(long time) {
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
/**
* @param time 时间
* @return 返回一个毫秒
*/
// 格式到毫秒
public static String getSMillon(long time) {
return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);
}
/**
* 在日期上增加数个整月
*
* @param date 日期
* @param n 要增加的月数
* @return 增加数个整月
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
/**
* 在日期上增加天数
*
* @param date 日期
* @param n 要增加的天数
* @return 增加之后的天数
*/
public static Date addDay(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, n);
return cal.getTime();
}
/**
* 获取距现在某一小时的时刻
*
* @param format 格式化时间的格式
* @param h 距现在的小时 例如:h=-1为上一个小时,h=1为下一个小时
* @return 获取距现在某一小时的时刻
*/
public static String getNextHour(String format, int h) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = new Date();
date.setTime(date.getTime() + h * 60 * 60 * 1000);
return sdf.format(date);
}
/**
* 获取时间戳
*
* @return 获取时间戳
*/
public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
}
/**
* 功能描述:返回月
*
* @param date Date 日期
* @return 返回月份
*/
public static int getMonth(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
}
/**
* 功能描述:返回日
*
* @param date Date 日期
* @return 返回日份
*/
public static int getDay(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
/**
* 功能描述:返回小
*
* @param date 日期
* @return 返回小时
*/
public static int getHour(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
}
/**
* 功能描述:返回分
*
* @param date 日期
* @return 返回分钟
*/
public static int getMinute(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MINUTE);
}
/**
* 获得默认的 date pattern
*
* @return 默认的格式
*/
public static String getDatePattern() {
return FORMAT_YMDHMS;
}
/**
* 返回秒钟
*
* @param date Date 日期
* @return 返回秒钟
*/
public static int getSecond(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.SECOND);
}
/**
* 使用预设格式提取字符串日期
*
* @param strDate 日期字符串
* @return 提取字符串的日期
*/
public static Date parse(String strDate) {
return parse(strDate, getDatePattern());
}
/**
* 功能描述:返回毫
*
* @param date 日期
* @return 返回毫
*/
public static long getMillis(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getTimeInMillis();
}
/**
* 按默认格式的字符串距离今天的天数
*
* @param date 日期字符串
* @return 按默认格式的字符串距离今天的天数
*/
public static int countDays(String date) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
/**
* 使用用户格式提取字符串日期
*
* @param strDate 日期字符串
* @param pattern 日期格式
* @return 提取字符串日期
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 按用户格式字符串距离今天的天数
*
* @param date 日期字符串
* @param format 日期格式
* @return 按用户格式字符串距离今天的天数
*/
public static int countDays(String date, String format) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date, format));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
/**
* 两个时间相差距离多少天多少小时多少分多少秒
*
* @param str1 时间参数 1 格式:1990-01-01 12:00:00
* @param str2 时间参数 2 格式:2009-01-01 12:00:00
* @return String 返回值为:xx天xx小时xx分xx秒
*/
public static String getDistanceTime(String str1, String str2) {
DateFormat df = new SimpleDateFormat(FORMAT);
Date one;
Date two;
try {
one = df.parse(str1);
two = df.parse(str2);
return getDistanceTime(one, two);
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
/**
* 获取时间差多少天多少小时多少分多少秒
*/
public static String getDistanceTime(Date one, Date two) {
long day;
long hour;
long min;
long sec;
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = ((diff / (1000)) - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
return String.format("%d天%02d小时%02d分钟%02d秒", day, hour, min, sec);
}
}
常用单位转换
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;
/**
* 常用单位转换的辅助类
*/
public class DensityUtils {
private DensityUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
private static int[] deviceWidthHeight = new int[2];
public static int[] getDeviceInfo(Context context) {
if ((deviceWidthHeight[0] == 0) && (deviceWidthHeight[1] == 0)) {
DisplayMetrics metrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay()
.getMetrics(metrics);
deviceWidthHeight[0] = metrics.widthPixels;
deviceWidthHeight[1] = metrics.heightPixels;
}
return deviceWidthHeight;
}
/**
* dp转px
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 获取屏幕尺寸
*/
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public static Point getScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
return new Point(display.getWidth(), display.getHeight());
} else {
Point point = new Point();
display.getSize(point);
return point;
}
}
/**
* px转dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* dp转px
*/
public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
/**
* sp转px
*/
public static int sp2px(Context context, float spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, context.getResources().getDisplayMetrics());
}
/**
* px转dp
*/
public static float px2dp(Context context, float pxVal) {
final float scale = context.getResources().getDisplayMetrics().density;
return (pxVal / scale);
}
/**
* px转sp
*/
public static float px2sp(Context context, float pxVal) {
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
}
}
DES加密解密
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
* 将字符串进行DES加密解密
*/
public class DES {
/** 加密KEY */
private static final byte[] KEY = "7;9Ku7;:84VG*B78".getBytes();
/** 算法 */
private static final String ALGORITHM = "DES";
/** IV */
private static final byte[] IV = "sHjrydLq".getBytes();
/** TRANSFORMATION */
private static final String TRANSFORMATION = "DES/CBC/PKCS5Padding";
private int code = 0;
public DES() {
}
/**
* 构造函数
* @param code 加密方式:0-“ISO-8859-1”编码,1-base64编码,其它-默认编码(utf-8)
*/
public DES(int code) {
this.code = code;
}
/**
* 将字符串进行DES加密
* @param source 未加密源字符串
* @return 加密后字符串
*/
public String encrypt(String source) {
byte[] retByte = null;
// Create SecretKey object
DESKeySpec dks = null;
try {
dks = new DESKeySpec(KEY);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
// Create IvParameterSpec object with initialization vector
IvParameterSpec spec = new IvParameterSpec(IV);
// Create Cipter object
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
// Initialize Cipher object
cipher.init(Cipher.ENCRYPT_MODE, securekey, spec);
// Decrypting data
retByte = cipher.doFinal(source.getBytes());
String result = "";
if (code == 0) {
result = new String(retByte, "ISO-8859-1");
} else if (code == 1) {
result = Base64.encodeToString(retByte,false);
} else {
result = new String(retByte);
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将DES加密的字符串解密
* @param encrypted 加密过的字符串
* @return 未加密源字符串
*/
public String decrypt(String encrypted) {
byte[] retByte = null;
// Create SecretKey object
DESKeySpec dks = null;
try {
dks = new DESKeySpec(KEY);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
// Create IvParameterSpec object with initialization vector
IvParameterSpec spec = new IvParameterSpec(IV);
// Create Cipter object
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
// Initialize Cipher object
cipher.init(Cipher.DECRYPT_MODE, securekey, spec);
if (code == 0) {
retByte = encrypted.getBytes("ISO-8859-1");
} else if (code == 1) {
retByte = Base64.decode(encrypted);
} else {
retByte = encrypted.getBytes();
}
// Decrypting data
retByte = cipher.doFinal(retByte);
return new String(retByte, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
编辑框数字过滤
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EditInputFilter implements InputFilter {
/**
* 最大数字
*/
public static final int MAX_VALUE = 10000000;
/**
* 小数点后的数字的位数
*/
public static final int PONTINT_LENGTH = 2;
Pattern p;
public EditInputFilter() {
p = Pattern.compile("[0-9]*"); //除数字外的其他的
}
/**
* source 新输入的字符串
* start 新输入的字符串起始下标,一般为0
* end 新输入的字符串终点下标,一般为source长度-1
* dest 输入之前文本框内容
* dstart 原内容起始坐标,一般为0
* dend 原内容终点坐标,一般为dest长度-1
*/
@Override
public CharSequence filter(CharSequence src, int start, int end,
Spanned dest, int dstart, int dend) {
String oldtext = dest.toString();
System.out.println(oldtext);
//验证删除等按键
if ("".equals(src.toString())) {
return null;
}
if ((oldtext + src.toString()).equals(".")) {
return "0.";
}
//验证非数字或者小数点的情况
Matcher m = p.matcher(src);
if (oldtext.contains(".")) {
//已经存在小数点的情况下,只能输入数字
if (!m.matches()) {
return null;
}
} else {
//未输入小数点的情况下,可以输入小数点和数字
if (!m.matches() && !src.equals(".")) {
return null;
}
}
//验证输入金额的大小
if (!TextUtils.isEmpty(src)) {
double dold = Double.parseDouble(oldtext + src.toString());
if (dold > MAX_VALUE) {
ToastUtil.showToast("超过最大限制" + MAX_VALUE);
return dest.subSequence(dstart, dend);
} else if (dold == MAX_VALUE) {
if (src.toString().equals(".")) {
ToastUtil.showToast("超过最大限制" + MAX_VALUE);
return dest.subSequence(dstart, dend);
}
}
}
//验证小数位精度是否正确
if (oldtext.contains(".")) {
int index = oldtext.indexOf(".");
int len = dend - index;
//小数位只能2位
if (len > PONTINT_LENGTH) {
CharSequence newText = dest.subSequence(dstart, dend);
return newText;
}
}
return dest.subSequence(dstart, dend) + src.toString();
}
}
文件工具
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class FileUtils {
public final static String FILE_EXTENSION_SEPARATOR = ".";
/**
* URI类型:file
*/
public static final String URI_TYPE_FILE = "file";
private FileUtils() {
throw new AssertionError();
}
/**
* read file
*
* @param filePath 路径
* @param charsetName The name of a supported {@link
* java.nio.charset.Charset </code>charset<code>}
* @return if file not exist, return null, else return content of file
* @throws RuntimeException if an error occurs while operator
* BufferedReader
*/
public static StringBuilder readFile(String filePath, String charsetName) {
File file = new File(filePath);
StringBuilder fileContent = new StringBuilder("");
if (file == null || !file.isFile()) {
return null;
}
BufferedReader reader = null;
try {
InputStreamReader is = new InputStreamReader(
new FileInputStream(file), charsetName);
reader = new BufferedReader(is);
String line = null;
while ((line = reader.readLine()) != null) {
if (!fileContent.toString().equals("")) {
fileContent.append("\r\n");
}
fileContent.append(line);
}
return fileContent;
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
} finally {
IOUtils.close(reader);
}
}
/**
* write file
*
* @param filePath 路径
* @param content 上下文
* @param append is append, if true, write to the end of file, else clear
* content of file and write into it
* @return return false if content is empty, true otherwise
* @throws RuntimeException if an error occurs while operator FileWriter
*/
public static boolean writeFile(String filePath, String content, boolean append) {
if (StringUtils.isEmpty(content)) {
return false;
}
FileWriter fileWriter = null;
try {
makeDirs(filePath);
fileWriter = new FileWriter(filePath, append);
fileWriter.write(content);
return true;
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
} finally {
IOUtils.close(fileWriter);
}
}
/**
* write file
*
* @param filePath 路径
* @param contentList 集合
* @param append is append, if true, write to the end of file, else clear
* content of file and write into it
* @return return false if contentList is empty, true otherwise
* @throws RuntimeException if an error occurs while operator FileWriter
*/
public static boolean writeFile(String filePath, List<String> contentList, boolean append) {
if (contentList.size() == 0 || null == contentList) {
return false;
}
FileWriter fileWriter = null;
try {
makeDirs(filePath);
fileWriter = new FileWriter(filePath, append);
int i = 0;
for (String line : contentList) {
if (i++ > 0) {
fileWriter.write("\r\n");
}
fileWriter.write(line);
}
return true;
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
} finally {
IOUtils.close(fileWriter);
}
}
/**
* write file, the string will be written to the begin of the file
*
* @param filePath 地址
* @param content 上下文
* @return 是否写入成功
*/
public static boolean writeFile(String filePath, String content) {
return writeFile(filePath, content, false);
}
/**
* write file, the string list will be written to the begin of the file
*
* @param filePath 地址
* @param contentList 集合
* @return 是否写入成功
*/
public static boolean writeFile(String filePath, List<String> contentList) {
return writeFile(filePath, contentList, false);
}
/**
* write file, the bytes will be written to the begin of the file
*
* @param filePath 路径
* @param stream 输入流
* @return 返回是否写入成功
*/
public static boolean writeFile(String filePath, InputStream stream) {
return writeFile(filePath, stream, false);
}
/**
* write file
*
* @param filePath 路径
* @param stream the input stream
* @param append if <code>true</code>, then bytes will be written to the
* end
* of the file rather than the beginning
* @return return true
* FileOutputStream
*/
public static boolean writeFile(String filePath, InputStream stream, boolean append) {
return writeFile(filePath != null ? new File(filePath) : null, stream,
append);
}
/**
* write file, the bytes will be written to the begin of the file
*
* @param file 文件对象
* @param stream 输入流
* @return 返回是否写入成功
*/
public static boolean writeFile(File file, InputStream stream) {
return writeFile(file, stream, false);
}
/**
* write file
*
* @param file the file to be opened for writing.
* @param stream the input stream
* @param append if <code>true</code>, then bytes will be written to the
* end
* of the file rather than the beginning
* @return return true
* @throws RuntimeException if an error occurs while operator
* FileOutputStream
*/
public static boolean writeFile(File file, InputStream stream, boolean append) {
OutputStream o = null;
try {
makeDirs(file.getAbsolutePath());
o = new FileOutputStream(file, append);
byte data[] = new byte[1024];
int length = -1;
while ((length = stream.read(data)) != -1) {
o.write(data, 0, length);
}
o.flush();
return true;
} catch (FileNotFoundException e) {
throw new RuntimeException("FileNotFoundException occurred. ", e);
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
} finally {
IOUtils.close(o);
IOUtils.close(stream);
}
}
/**
* move file
*
* @param sourceFilePath 资源路径
* @param destFilePath 删除的路径
*/
public static void moveFile(String sourceFilePath, String destFilePath) {
if (TextUtils.isEmpty(sourceFilePath) ||
TextUtils.isEmpty(destFilePath)) {
throw new RuntimeException(
"Both sourceFilePath and destFilePath cannot be null.");
}
moveFile(new File(sourceFilePath), new File(destFilePath));
}
/**
* move file
*
* @param srcFile 文件对象
* @param destFile 对象
*/
public static void moveFile(File srcFile, File destFile) {
boolean rename = srcFile.renameTo(destFile);
if (!rename) {
copyFile(srcFile.getAbsolutePath(), destFile.getAbsolutePath());
deleteFile(srcFile.getAbsolutePath());
}
}
/**
* copy file
*
* @param sourceFilePath 资源路径
* @param destFilePath 删除的文件
* @return 返回是否成功
* @throws RuntimeException if an error occurs while operator
* FileOutputStream
*/
public static boolean copyFile(String sourceFilePath, String destFilePath) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(sourceFilePath);
} catch (FileNotFoundException e) {
throw new RuntimeException("FileNotFoundException occurred. ", e);
}
return writeFile(destFilePath, inputStream);
}
/**
* read file to string list, a element of list is a line
*
* @param filePath 路径
* @param charsetName The name of a supported {@link
* java.nio.charset.Charset </code>charset<code>}
* @return if file not exist, return null, else return content of file
* @throws RuntimeException if an error occurs while operator
* BufferedReader
*/
public static List<String> readFileToList(String filePath, String charsetName) {
File file = new File(filePath);
List<String> fileContent = new ArrayList<String>();
if (file == null || !file.isFile()) {
return null;
}
BufferedReader reader = null;
try {
InputStreamReader is = new InputStreamReader(
new FileInputStream(file), charsetName);
reader = new BufferedReader(is);
String line = null;
while ((line = reader.readLine()) != null) {
fileContent.add(line);
}
return fileContent;
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
} finally {
IOUtils.close(reader);
}
}
/**
* @param filePath 文件的路径
* @return 返回文件的信息
*/
public static String getFileNameWithoutExtension(String filePath) {
if (StringUtils.isEmpty(filePath)) {
return filePath;
}
int extenPosi = filePath.lastIndexOf(FILE_EXTENSION_SEPARATOR);
int filePosi = filePath.lastIndexOf(File.separator);
if (filePosi == -1) {
return (extenPosi == -1
? filePath
: filePath.substring(0, extenPosi));
}
if (extenPosi == -1) {
return filePath.substring(filePosi + 1);
}
return (filePosi < extenPosi ? filePath.substring(filePosi + 1,
extenPosi) : filePath.substring(filePosi + 1));
}
/**
* get file name from path, include suffix
* <p/>
* <pre>
* getFileName(null) = null
* getFileName("") = ""
* getFileName(" ") = " "
* getFileName("a.mp3") = "a.mp3"
* getFileName("a.b.rmvb") = "a.b.rmvb"
* getFileName("abc") = "abc"
* getFileName("c:\\") = ""
* getFileName("c:\\a") = "a"
* getFileName("c:\\a.b") = "a.b"
* getFileName("c:a.txt\\a") = "a"
* getFileName("/home/admin") = "admin"
* getFileName("/home/admin/a.txt/b.mp3") = "b.mp3"
* </pre>
*
* @param filePath 路径
* @return file name from path, include suffix
*/
public static String getFileName(String filePath) {
if (StringUtils.isEmpty(filePath)) {
return filePath;
}
int filePosi = filePath.lastIndexOf(File.separator);
return (filePosi == -1) ? filePath : filePath.substring(filePosi + 1);
}
/**
* get folder name from path
* <p/>
* <pre>
* getFolderName(null) = null
* getFolderName("") = ""
* getFolderName(" ") = ""
* getFolderName("a.mp3") = ""
* getFolderName("a.b.rmvb") = ""
* getFolderName("abc") = ""
* getFolderName("c:\\") = "c:"
* getFolderName("c:\\a") = "c:"
* getFolderName("c:\\a.b") = "c:"
* getFolderName("c:a.txt\\a") = "c:a.txt"
* getFolderName("c:a\\b\\c\\d.txt") = "c:a\\b\\c"
* getFolderName("/home/admin") = "/home"
* getFolderName("/home/admin/a.txt/b.mp3") = "/home/admin/a.txt"
* </pre>
*
* @param filePath 路径
* @return file name from path, include suffix
*/
public static String getFolderName(String filePath) {
if (StringUtils.isEmpty(filePath)) {
return filePath;
}
int filePosi = filePath.lastIndexOf(File.separator);
return (filePosi == -1) ? "" : filePath.substring(0, filePosi);
}
/**
* get suffix of file from path
* <p/>
* <pre>
* getFileExtension(null) = ""
* getFileExtension("") = ""
* getFileExtension(" ") = " "
* getFileExtension("a.mp3") = "mp3"
* getFileExtension("a.b.rmvb") = "rmvb"
* getFileExtension("abc") = ""
* getFileExtension("c:\\") = ""
* getFileExtension("c:\\a") = ""
* getFileExtension("c:\\a.b") = "b"
* getFileExtension("c:a.txt\\a") = ""
* getFileExtension("/home/admin") = ""
* getFileExtension("/home/admin/a.txt/b") = ""
* getFileExtension("/home/admin/a.txt/b.mp3") = "mp3"
* </pre>
*
* @param filePath 路径
* @return 信息
*/
public static String getFileExtension(String filePath) {
if (StringUtils.isBlank(filePath)) {
return filePath;
}
int extenPosi = filePath.lastIndexOf(FILE_EXTENSION_SEPARATOR);
int filePosi = filePath.lastIndexOf(File.separator);
if (extenPosi == -1) {
return "";
}
return (filePosi >= extenPosi) ? "" : filePath.substring(extenPosi + 1);
}
/**
* @param filePath 路径
* @return 是否创建成功
*/
public static boolean makeDirs(String filePath) {
String folderName = getFolderName(filePath);
if (StringUtils.isEmpty(folderName)) {
return false;
}
File folder = new File(folderName);
return (folder.exists() && folder.isDirectory()) || folder.mkdirs();
}
/**
* @param filePath 路径
* @return 是否创建成功
*/
public static boolean makeFolders(String filePath) {
return makeDirs(filePath);
}
/**
* @param filePath 路径
* @return 是否存在这个文件
*/
public static boolean isFileExist(String filePath) {
if (StringUtils.isBlank(filePath)) {
return false;
}
File file = new File(filePath);
return (file.exists() && file.isFile());
}
/**
* @param directoryPath 路径
* @return 是否有文件夹
*/
public static boolean isFolderExist(String directoryPath) {
if (StringUtils.isBlank(directoryPath)) {
return false;
}
File dire = new File(directoryPath);
return (dire.exists() && dire.isDirectory());
}
/**
* @param path 路径
* @return 是否删除成功
*/
public static boolean deleteFile(String path) {
if (StringUtils.isBlank(path)) {
return true;
}
File file = new File(path);
if (!file.exists()) {
return true;
}
if (file.isFile()) {
return file.delete();
}
if (!file.isDirectory()) {
return false;
}
for (File f : file.listFiles()) {
if (f.isFile()) {
f.delete();
} else if (f.isDirectory()) {
deleteFile(f.getAbsolutePath());
}
}
return file.delete();
}
/**
* @param path 路径
* @return 返回文件大小
*/
public static long getFileSize(String path) {
if (StringUtils.isBlank(path)) {
return -1;
}
File file = new File(path);
return (file.exists() && file.isFile() ? file.length() : -1);
}
/**
* 文件重命名
*
* @param oldPath 旧的文件名字
* @param newPath 新的文件名字
*/
public static void renameFile(String oldPath, String newPath) {
try {
if (!TextUtils.isEmpty(oldPath) && !TextUtils.isEmpty(newPath)
&& !oldPath.equals(newPath)) {
File fileOld = new File(oldPath);
File fileNew = new File(newPath);
fileOld.renameTo(fileNew);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
IO操作
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* IO操作
*/
public class IOUtils {
private IOUtils() {
throw new AssertionError();
}
/**
* Close closable object and wrap {@link IOException} with {@link
* RuntimeException}
*
* @param closeable closeable object
*/
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
}
}
}
/**
* Close closable and hide possible {@link IOException}
*
* @param closeable closeable object
*/
public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
// Ignored
}
}
}
/**
* 保存文本
*
* @param fileName 文件名字
* @param content 内容
* @param append 是否累加
* @return 是否成功
*/
public static boolean saveTextValue(String fileName, String content, boolean append) {
try {
File textFile = new File(fileName);
if (!append && textFile.exists()) textFile.delete();
FileOutputStream os = new FileOutputStream(textFile);
os.write(content.getBytes("UTF-8"));
os.close();
} catch (Exception ee) {
return false;
}
return true;
}
/**
* 删除目录下所有文件
*
* @param Path 路径
*/
public static void deleteAllFile(String Path) {
// 删除目录下所有文件
File path = new File(Path);
File files[] = path.listFiles();
if (files != null) {
for (File tfi : files) {
if (tfi.isDirectory()) {
System.out.println(tfi.getName());
} else {
tfi.delete();
}
}
}
}
}
软键盘
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* 打开或关闭软键盘
*/
public class KeyBoardUtils {
/**
* 打卡软键盘
*
* @param mEditText 输入框
* @param mContext 上下文
*/
public static void showSoftInput(Context mContext, EditText mEditText) {
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
/**
* 关闭软键盘
*
* @param mEditText 输入框
* @param mContext 上下文
*/
public static void hideSoftInput(Context mContext, EditText mEditText) {
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
public static boolean isShowSoftInput(Context context){
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
//获取状态信息
return imm.isActive();//true 打开
}
}
日志
public class Log {
private static String sTag = "app";
private static boolean sEnabled = true;
private Log() {
}
public static boolean isEnabled() {
return sEnabled;
}
public static void setEnabled(boolean enabled) {
sEnabled = enabled;
}
public static boolean isLoggingEnabled() {
return sEnabled;
}
public static int v(String msg) {
return sEnabled?android.util.Log.v(sTag, msg):0;
}
public static int v(String tag, String msg) {
return sEnabled?android.util.Log.v(tag, msg):0;
}
public static int v(String msg, Throwable tr) {
return sEnabled?android.util.Log.v(sTag, msg, tr):0;
}
public static int v(String tag, String msg, Throwable tr) {
return sEnabled?android.util.Log.v(tag, msg, tr):0;
}
public static int d(String msg) {
return sEnabled?android.util.Log.d(sTag, msg):0;
}
public static int d(String tag, String msg) {
return sEnabled?android.util.Log.d(tag, msg):0;
}
public static int d(String msg, Throwable tr) {
return sEnabled?android.util.Log.d(sTag, msg, tr):0;
}
public static int d(String tag, String msg, Throwable tr) {
return sEnabled?android.util.Log.d(tag, msg, tr):0;
}
public static int i(String msg) {
return sEnabled?android.util.Log.i(sTag, msg):0;
}
public static int i(String tag, String msg) {
return sEnabled?android.util.Log.i(tag, msg):0;
}
public static int i(String msg, Throwable tr) {
return sEnabled?android.util.Log.i(sTag, msg, tr):0;
}
public static int i(String tag, String msg, Throwable tr) {
return sEnabled?android.util.Log.i(tag, msg, tr):0;
}
public static int w(String msg) {
return sEnabled?android.util.Log.w(sTag, msg):0;
}
public static int w(String tag, String msg) {
return sEnabled?android.util.Log.w(tag, msg):0;
}
public static int w(String msg, Throwable tr) {
return sEnabled?android.util.Log.w(sTag, msg, tr):0;
}
public static int w(String tag, String msg, Throwable tr) {
return sEnabled?android.util.Log.w(tag, msg, tr):0;
}
public static int e(String msg) {
return sEnabled?android.util.Log.e(sTag, msg):0;
}
public static int e(String tag, String msg) {
return sEnabled?android.util.Log.e(tag, msg):0;
}
public static int e(String msg, Throwable tr) {
return sEnabled?android.util.Log.e(sTag, msg, tr):0;
}
public static int e(String tag, String msg, Throwable tr) {
return sEnabled?android.util.Log.e(tag, msg, tr):0;
}
public static int t(String msg, Object... args) {
return sEnabled?android.util.Log.v("test", String.format(msg, args)):0;
}
}
网络相关
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* 跟网络相关的工具类
*/
public class NetUtils {
private NetUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 判断网络是否连接
*
* @param context
* @return
*/
public static boolean isConnected(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivity) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
/**
* 判断是否是wifi连接
*/
public static boolean isWifi(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null)
return false;
return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
}
/**
* 打开网络设置界面
*/
public static void openSetting(Activity activity) {
Intent intent = new Intent("/");
ComponentName cm = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
intent.setComponent(cm);
intent.setAction("android.intent.action.VIEW");
activity.startActivityForResult(intent, 0);
}
}
手机组件调用
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.telephony.TelephonyManager;
import java.io.File;
/**
* 手机组件调用工具类
*
* @author jingle1267@163.com
*/
public final class PhoneUtil {
private static long lastClickTime;
/**
* Don't let anyone instantiate this class.
*/
private PhoneUtil() {
throw new Error("Do not need instantiate!");
}
/**
* 调用系统发短信界面
*
* @param activity Activity
* @param phoneNumber 手机号码
* @param smsContent 短信内容
*/
public static void sendMessage(Context activity, String phoneNumber, String smsContent) {
if (phoneNumber == null || phoneNumber.length() < 4) {
return;
}
Uri uri = Uri.parse("smsto:" + phoneNumber);
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", smsContent);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(it);
}
/**
* 判断是否为连击
*
* @return boolean
*/
public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if (0 < timeD && timeD < 500) {
return true;
}
lastClickTime = time;
return false;
}
/**
* 获取手机型号
*
* @param context 上下文
* @return String
*/
public static String getMobileModel(Context context) {
try {
String model = android.os.Build.MODEL; // 手机型号
return model;
} catch (Exception e) {
return "未知";
}
}
/**
* 获取手机品牌
*
* @param context 上下文
* @return String
*/
public static String getMobileBrand(Context context) {
try {
String brand = android.os.Build.BRAND; // android系统版本号
return brand;
} catch (Exception e) {
return "未知";
}
}
/**
* 拍照打开照相机!
*
* @param requestcode 返回值
* @param activity 上下文
* @param fileName 生成的图片文件的路径
*/
public static void toTakePhoto(int requestcode, Activity activity, String fileName) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("camerasensortype", 2);// 调用前置摄像头
intent.putExtra("autofocus", true);// 自动对焦
intent.putExtra("fullScreen", false);// 全屏
intent.putExtra("showActionIcons", false);
try {//创建一个当前任务id的文件然后里面存放任务的照片的和路径!这主文件的名字是用uuid到时候在用任务id去查路径!
File file = new File(fileName);
Uri uri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
activity.startActivityForResult(intent, requestcode);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打开相册
*
* @param requestcode 响应码
* @param activity 上下文
*/
public static void toTakePicture(int requestcode, Activity activity) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
activity.startActivityForResult(intent, requestcode);
}
/**
* 获取所有联系人的姓名和电话号码,需要READ_CONTACTS权限
*
* @param context 上下文
* @return Cursor。姓名:CommonDataKinds.Phone.DISPLAY_NAME;号码:CommonDataKinds.Phone.NUMBER
*/
public static Cursor getContactsNameAndNumber(Context context) {
return context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
/**
* 获取手机号码
*
* @param context 上下文
* @return 手机号码,手机号码不一定能获取到
*/
public static String getMobilePhoneNumber(Context context) {
return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
}
}
正则表达式
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则表达式验证
*/
public final class RegExpValidatorUtils {
public static boolean isUserName(String str) {
String regex = "^([a-zA-Z0-9\\u4e00-\\u9fa5]|[_]){4,12}$";
return match(regex, str);
}
/**
* 验证邮箱
*/
public static boolean isEmail(String str) {
String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
return match(regex, str);
}
/**
* 验证输入密码条件(字符与数据同时出现)
*/
public static boolean isPassword(String str) {
String regex = "[A-Za-z]+[0-9]";
return match(regex, str);
}
/**
* 验证输入密码长度 (6-18位)
*/
public static boolean isPasswLength(String str) {
String regex = "^\\w{6,18}$";
return match(regex, str);
}
/**
* 验证输入手机号码
*/
public static boolean isPhone(String str) {
String regex = "^1[3|4|5|7|8]\\d{9}$";
return match(regex, str);
}
/**
* 验证输入身份证号
*/
public static boolean isIDcard(String str) {
String regex = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
//String regex = "/^([1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])(\\d{4}|(\\d{3}(\\d|X|x))))$/";
//String regex = "^(([1-9]\\d{5}) ((19\\d{2})|(20[0,1]\\d)) ((0\\d)|(1[0-2])) (([0|1|2]\\d)|(3[0-1])) ((\\d{4})|(\\d{3}(\\d|X|x))) )$";
return match(regex, str);
}
/**
* 验证验证输入中文名
*/
public static boolean isChinese(String str) {
String regex = "^[\u4e00-\u9fa5]{2,6}$";
return match(regex, str);
}
private static boolean match(String regex, String str) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
}
屏幕相关
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
/**
* 获得屏幕相关的辅助类
*/
public class ScreenUtils {
private ScreenUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 获得屏幕高度
*/
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/**
* 获得屏幕宽度
*/
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
/**
* 获得状态栏的高度
*/
public static int getStatusHeight(Context context) {
int statusHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusHeight;
}
/**
* 获取当前屏幕截图,包含状态栏
*/
public static Bitmap snapShotWithStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}
/**
* 获取当前屏幕截图,不包含状态栏
*/
public static Bitmap snapShotWithoutStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return bp;
}
}
SD卡相关
import android.os.Environment;
import android.os.StatFs;
import java.io.File;
/**
* SD卡相关的辅助类
*/
public class SDCardUtils {
private SDCardUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 判断SDCard是否可用
*/
public static boolean isSDCardEnable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 获取SD卡路径
*/
public static String getSDCardPath() {
return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
}
/**
* 获取SD卡的剩余容量 单位byte
*/
public static long getSDCardAllSize() {
if (isSDCardEnable()) {
StatFs stat = new StatFs(getSDCardPath());
// 获取空闲的数据块的数量
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
// 获取单个数据块的大小(byte)
long freeBlocks = stat.getAvailableBlocks();
return freeBlocks * availableBlocks;
}
return 0;
}
/**
* 获取指定路径所在空间的剩余可用容量字节数,单位byte
*
* @param filePath
* @return 容量字节 SDCard可用空间,内部存储可用空间
*/
public static long getFreeBytes(String filePath) {
// 如果是sd卡的下的路径,则获取sd卡可用容量
if (filePath.startsWith(getSDCardPath())) {
filePath = getSDCardPath();
} else {// 如果是内部存储的路径,则获取内存存储的可用容量
filePath = Environment.getDataDirectory().getAbsolutePath();
}
StatFs stat = new StatFs(filePath);
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
return stat.getBlockSize() * availableBlocks;
}
/**
* 获取系统存储路径
*/
public static String getRootDirectoryPath() {
return Environment.getRootDirectory().getAbsolutePath();
}
}
快捷图标
import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.database.Cursor;
import android.net.Uri;
/**
* 创建删除快捷图标
* 需要权限: com.android.launcher.permission.INSTALL_SHORTCUT com.android.launcher.permission.UNINSTALL_SHORTCUT
*/
public final class ShortCutUtils {
/**
* Don't let anyone instantiate this class.
*/
private ShortCutUtils() {
throw new Error("Do not need instantiate!");
}
/**
* 检测是否存在快捷键
*
* @param activity Activity
* @return 是否存在桌面图标
*/
public static boolean hasShortcut(Activity activity) {
boolean isInstallShortcut = false;
final ContentResolver cr = activity.getContentResolver();
final String AUTHORITY = "com.android.launcher.settings";
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
Cursor c = cr.query(CONTENT_URI,
new String[]{"title", "iconResource"}, "title=?",
new String[]{activity.getString(R.string.app_name).trim()},
null);
if (c != null && c.getCount() > 0) {
isInstallShortcut = true;
}
return isInstallShortcut;
}
/**
* 为程序创建桌面快捷方式
*
* @param activity Activity
* @param res res
*/
public static void addShortcut(Activity activity, int res) {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
activity.getString(R.string.app_name));
shortcut.putExtra("duplicate", false); // 不允许重复创建
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(activity, activity.getClass().getName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
activity, res);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
activity.sendBroadcast(shortcut);
}
/**
* 删除程序的快捷方式
*
* @param activity Activity
*/
public static void delShortcut(Activity activity) {
Intent shortcut = new Intent(
"com.android.launcher.action.UNINSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
activity.getString(R.string.app_name));
String appClass = activity.getPackageName() + "."
+ activity.getLocalClassName();
ComponentName comp = new ComponentName(activity.getPackageName(),
appClass);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
Intent.ACTION_MAIN).setComponent(comp));
activity.sendBroadcast(shortcut);
}
}
SharedPreferences
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
public class SPUtil {
/**
* 保存在手机里面的文件名
*/
private static final String FILE_NAME = "share_data";
private static final String SPLIT = ",";
private static Context context;
public static void init(Context c){
context = c;
}
/**
* 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
*/
public static void put(String key, Object object) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean) {
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
SharedPreferencesCompat.apply(editor);
}
/**
* 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
*/
public static Object get(String key, Object defaultObject) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
if (defaultObject instanceof String) {
return sp.getString(key, (String) defaultObject);
} else if (defaultObject instanceof Integer) {
return sp.getInt(key, (Integer) defaultObject);
} else if (defaultObject instanceof Boolean) {
return sp.getBoolean(key, (Boolean) defaultObject);
} else if (defaultObject instanceof Float) {
return sp.getFloat(key, (Float) defaultObject);
} else if (defaultObject instanceof Long) {
return sp.getLong(key, (Long) defaultObject);
}
return null;
}
public static Object[] getList(String key, Object defaultObject) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
String data = sp.getString(key, "");
if (TextUtils.isEmpty(data)) {
return new Object[0];
}
String[] strings = data.split(SPLIT);
Object[] list = new Object[strings.length];
int index = 0;
for (String s : strings) {
if (defaultObject instanceof String) {
list[index++] = s;
} else if (defaultObject instanceof Integer) {
list[index++] = Integer.parseInt(s);
} else if (defaultObject instanceof Boolean) {
list[index++] = Boolean.parseBoolean(s);
} else if (defaultObject instanceof Float) {
list[index++] = Float.parseFloat(s);
} else if (defaultObject instanceof Long) {
list[index++] = Long.parseLong(s);
}
}
return list;
}
public static void putList(String key, Object[] value) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
String data = "";
for (Object v : value) {
data += v + SPLIT;
}
data = data.substring(0, data.length() - 1);
editor.putString(key, data);
SharedPreferencesCompat.apply(editor);
}
/**
* 移除某个key值已经对应的值
*/
public static void remove(String key) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.remove(key);
SharedPreferencesCompat.apply(editor);
}
/**
* 清除所有数据
*/
public static void clear() {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
SharedPreferencesCompat.apply(editor);
}
/**
* 查询某个key是否已经存在
*/
public static boolean contains(String key) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
return sp.contains(key);
}
/**
* 返回所有的键值对
*/
public static Map<String, ?> getAll() {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
return sp.getAll();
}
/**
* 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
*
* @author zhy
*/
private static class SharedPreferencesCompat {
private static final Method sApplyMethod = findApplyMethod();
/**
* 反射查找apply的方法
*/
@SuppressWarnings({"unchecked", "rawtypes"})
private static Method findApplyMethod() {
try {
Class clz = SharedPreferences.Editor.class;
return clz.getMethod("apply");
} catch (NoSuchMethodException e) {
}
return null;
}
/**
* 如果找到则使用apply执行,否则使用commit
*/
public static void apply(SharedPreferences.Editor editor) {
try {
if (sApplyMethod != null) {
sApplyMethod.invoke(editor);
return;
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
editor.commit();
}
}
}
字符串工具
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字符串工具类,提供一些字符串相关的便捷方法
*/
public class StringUtils {
private StringUtils() {
throw new AssertionError();
}
/**
* is null or its length is 0 or it is made by space
* <p/>
* <pre>
* isBlank(null) = true;
* isBlank("") = true;
* isBlank(" ") = true;
* isBlank("a") = false;
* isBlank("a ") = false;
* isBlank(" a") = false;
* isBlank("a b") = false;
* </pre>
*
* @param str str
* @return if string is null or its size is 0 or it is made by space, return
* true, else return false.
*/
public static boolean isBlank(String str) {
return (str == null || str.trim().length() == 0);
}
/**
* is null or its length is 0
* <p/>
* <pre>
* isEmpty(null) = true;
* isEmpty("") = true;
* isEmpty(" ") = false;
* </pre>
*
* @param str str
* @return if string is null or its size is 0, return true, else return
* false.
*/
public static boolean isEmpty(CharSequence str) {
return (str == null || str.length() == 0);
}
/**
* get length of CharSequence
* <p/>
* <pre>
* length(null) = 0;
* length(\"\") = 0;
* length(\"abc\") = 3;
* </pre>
*
* @param str str
* @return if str is null or empty, return 0, else return {@link
* CharSequence#length()}.
*/
public static int length(CharSequence str) {
return str == null ? 0 : str.length();
}
/**
* null Object to empty string
* <p/>
* <pre>
* nullStrToEmpty(null) = "";
* nullStrToEmpty("") = "";
* nullStrToEmpty("aa") = "aa";
* </pre>
*
* @param str str
* @return String
*/
public static String nullStrToEmpty(Object str) {
return (str == null ? "" : (str instanceof String ? (String) str : str.toString()));
}
/**
* @param str str
* @return String
*/
public static String capitalizeFirstLetter(String str) {
if (isEmpty(str)) {
return str;
}
char c = str.charAt(0);
return (!Character.isLetter(c) || Character.isUpperCase(c))
? str
: new StringBuilder(str.length()).append(
Character.toUpperCase(c))
.append(str.substring(1))
.toString();
}
/**
* encoded in utf-8
*
* @param str 字符串
* @return 返回一个utf8的字符串
*/
public static String utf8Encode(String str) {
if (!isEmpty(str) && str.getBytes().length != str.length()) {
try {
return URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(
"UnsupportedEncodingException occurred. ", e);
}
}
return str;
}
/**
* @param href 字符串
* @return 返回一个html
*/
public static String getHrefInnerHtml(String href) {
if (isEmpty(href)) {
return "";
}
String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*";
Pattern hrefPattern = Pattern.compile(hrefReg,
Pattern.CASE_INSENSITIVE);
Matcher hrefMatcher = hrefPattern.matcher(href);
if (hrefMatcher.matches()) {
return hrefMatcher.group(1);
}
return href;
}
/**
* @param source 字符串
* @return 返回htmL到字符串
*/
public static String htmlEscapeCharsToString(String source) {
return StringUtils.isEmpty(source)
? source
: source.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll("&", "&")
.replaceAll(""", "\"");
}
/**
* @param s str
* @return String
*/
public static String fullWidthToHalfWidth(String s) {
if (isEmpty(s)) {
return s;
}
char[] source = s.toCharArray();
for (int i = 0; i < source.length; i++) {
if (source[i] == 12288) {
source[i] = ' ';
// } else if (source[i] == 12290) {
// source[i] = '.';
} else if (source[i] >= 65281 && source[i] <= 65374) {
source[i] = (char) (source[i] - 65248);
} else {
source[i] = source[i];
}
}
return new String(source);
}
/**
* @param s 字符串
* @return 返回的数值
*/
public static String halfWidthToFullWidth(String s) {
if (isEmpty(s)) {
return s;
}
char[] source = s.toCharArray();
for (int i = 0; i < source.length; i++) {
if (source[i] == ' ') {
source[i] = (char) 12288;
// } else if (source[i] == '.') {
// source[i] = (char)12290;
} else if (source[i] >= 33 && source[i] <= 126) {
source[i] = (char) (source[i] + 65248);
} else {
source[i] = source[i];
}
}
return new String(source);
}
/**
* @param str 资源
* @return 特殊字符串切换
*/
public static String replaceBlanktihuan(String str) {
String dest = "";
if (str != null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
/**
* 判断给定的字符串是否为null或者是空的
*
* @param string 给定的字符串
*/
public static boolean isEmpty(String string) {
return string == null || "".equals(string.trim());
}
/**
* 判断给定的字符串是否不为null且不为空
*
* @param string 给定的字符串
*/
public static boolean isNotEmpty(String string) {
return !isEmpty(string);
}
/**
* 判断给定的字符串数组中的所有字符串是否都为null或者是空的
*
* @param strings 给定的字符串
*/
public static boolean isEmpty(String... strings) {
boolean result = true;
for (String string : strings) {
if (isNotEmpty(string)) {
result = false;
break;
}
}
return result;
}
/**
* 判断给定的字符串数组中是否全部都不为null且不为空
*
* @param strings 给定的字符串数组
* @return 是否全部都不为null且不为空
*/
public static boolean isNotEmpty(String... strings) {
boolean result = true;
for (String string : strings) {
if (isEmpty(string)) {
result = false;
break;
}
}
return result;
}
/**
* 如果字符串是null或者空就返回""
*/
public static String filterEmpty(String string) {
return StringUtils.isNotEmpty(string) ? string : "";
}
/**
* 在给定的字符串中,用新的字符替换所有旧的字符
*
* @param string 给定的字符串
* @param oldchar 旧的字符
* @param newchar 新的字符
* @return 替换后的字符串
*/
public static String replace(String string, char oldchar, char newchar) {
char chars[] = string.toCharArray();
for (int w = 0; w < chars.length; w++) {
if (chars[w] == oldchar) {
chars[w] = newchar;
break;
}
}
return new String(chars);
}
/**
* 把给定的字符串用给定的字符分割
*
* @param string 给定的字符串
* @param ch 给定的字符
* @return 分割后的字符串数组
*/
public static String[] split(String string, char ch) {
ArrayList<String> stringList = new ArrayList<String>();
char chars[] = string.toCharArray();
int nextStart = 0;
for (int w = 0; w < chars.length; w++) {
if (ch == chars[w]) {
stringList.add(new String(chars, nextStart, w - nextStart));
nextStart = w + 1;
if (nextStart ==
chars.length) { //当最后一位是分割符的话,就再添加一个空的字符串到分割数组中去
stringList.add("");
}
}
}
if (nextStart <
chars.length) { //如果最后一位不是分隔符的话,就将最后一个分割符到最后一个字符中间的左右字符串作为一个字符串添加到分割数组中去
stringList.add(new String(chars, nextStart,
chars.length - 1 - nextStart + 1));
}
return stringList.toArray(new String[stringList.size()]);
}
/**
* 计算给定的字符串的长度,计算规则是:一个汉字的长度为2,一个字符的长度为1
*
* @param string 给定的字符串
* @return 长度
*/
public static int countLength(String string) {
int length = 0;
char[] chars = string.toCharArray();
for (int w = 0; w < string.length(); w++) {
char ch = chars[w];
if (ch >= '\u0391' && ch <= '\uFFE5') {
length++;
length++;
} else {
length++;
}
}
return length;
}
private static char[] getChars(char[] chars, int startIndex) {
int endIndex = startIndex + 1;
//如果第一个是数字
if (Character.isDigit(chars[startIndex])) {
//如果下一个是数字
while (endIndex < chars.length &&
Character.isDigit(chars[endIndex])) {
endIndex++;
}
}
char[] resultChars = new char[endIndex - startIndex];
System.arraycopy(chars, startIndex, resultChars, 0, resultChars.length);
return resultChars;
}
/**
* 是否全是数字
*/
public static boolean isAllDigital(char[] chars) {
boolean result = true;
for (int w = 0; w < chars.length; w++) {
if (!Character.isDigit(chars[w])) {
result = false;
break;
}
}
return result;
}
/**
* 删除给定字符串中所有的旧的字符
*
* @param string 源字符串
* @param ch 要删除的字符
* @return 删除后的字符串
*/
public static String removeChar(String string, char ch) {
StringBuffer sb = new StringBuffer();
for (char cha : string.toCharArray()) {
if (cha != '-') {
sb.append(cha);
}
}
return sb.toString();
}
/**
* 删除给定字符串中给定位置处的字符
*
* @param string 给定字符串
* @param index 给定位置
*/
public static String removeChar(String string, int index) {
String result = null;
char[] chars = string.toCharArray();
if (index == 0) {
result = new String(chars, 1, chars.length - 1);
} else if (index == chars.length - 1) {
result = new String(chars, 0, chars.length - 1);
} else {
result = new String(chars, 0, index) +
new String(chars, index + 1, chars.length - index);
}
return result;
}
/**
* 删除给定字符串中给定位置处的字符
*
* @param string 给定字符串
* @param index 给定位置
* @param ch 如果同给定位置处的字符相同,则将给定位置处的字符删除
*/
public static String removeChar(String string, int index, char ch) {
String result = null;
char[] chars = string.toCharArray();
if (chars.length > 0 && chars[index] == ch) {
if (index == 0) {
result = new String(chars, 1, chars.length - 1);
} else if (index == chars.length - 1) {
result = new String(chars, 0, chars.length - 1);
} else {
result = new String(chars, 0, index) +
new String(chars, index + 1, chars.length - index);
}
} else {
result = string;
}
return result;
}
/**
* 对给定的字符串进行空白过滤
*
* @param string 给定的字符串
* @return 如果给定的字符串是一个空白字符串,那么返回null;否则返回本身。
*/
public static String filterBlank(String string) {
if ("".equals(string)) {
return null;
} else {
return string;
}
}
/**
* 将给定字符串中给定的区域的字符转换成小写
*
* @param str 给定字符串中
* @param beginIndex 开始索引(包括)
* @param endIndex 结束索引(不包括)
* @return 新的字符串
*/
public static String toLowerCase(String str, int beginIndex, int endIndex) {
return str.replaceFirst(str.substring(beginIndex, endIndex),
str.substring(beginIndex, endIndex)
.toLowerCase(Locale.getDefault()));
}
/**
* 将给定字符串中给定的区域的字符转换成大写
*
* @param str 给定字符串中
* @param beginIndex 开始索引(包括)
* @param endIndex 结束索引(不包括)
* @return 新的字符串
*/
public static String toUpperCase(String str, int beginIndex, int endIndex) {
return str.replaceFirst(str.substring(beginIndex, endIndex),
str.substring(beginIndex, endIndex)
.toUpperCase(Locale.getDefault()));
}
/**
* 将给定字符串的首字母转为小写
*
* @param str 给定字符串
* @return 新的字符串
*/
public static String firstLetterToLowerCase(String str) {
return toLowerCase(str, 0, 1);
}
/**
* 将给定字符串的首字母转为大写
*
* @param str 给定字符串
* @return 新的字符串
*/
public static String firstLetterToUpperCase(String str) {
return toUpperCase(str, 0, 1);
}
/**
* 将给定的字符串MD5加密
*
* @param string 给定的字符串
* @return MD5加密后生成的字符串
*/
public static String MD5(String string) {
String result = null;
try {
char[] charArray = string.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++) {
byteArray[i] = (byte) charArray[i];
}
StringBuffer hexValue = new StringBuffer();
byte[] md5Bytes = MessageDigest.getInstance("MD5")
.digest(byteArray);
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
result = hexValue.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 判断给定的字符串是否以一个特定的字符串开头,忽略大小写
*
* @param sourceString 给定的字符串
* @param newString 一个特定的字符串
*/
public static boolean startsWithIgnoreCase(String sourceString, String newString) {
int newLength = newString.length();
int sourceLength = sourceString.length();
if (newLength == sourceLength) {
return newString.equalsIgnoreCase(sourceString);
} else if (newLength < sourceLength) {
char[] newChars = new char[newLength];
sourceString.getChars(0, newLength, newChars, 0);
return newString.equalsIgnoreCase(String.valueOf(newChars));
} else {
return false;
}
}
/**
* 判断给定的字符串是否以一个特定的字符串结尾,忽略大小写
*
* @param sourceString 给定的字符串
* @param newString 一个特定的字符串
*/
public static boolean endsWithIgnoreCase(String sourceString, String newString) {
int newLength = newString.length();
int sourceLength = sourceString.length();
if (newLength == sourceLength) {
return newString.equalsIgnoreCase(sourceString);
} else if (newLength < sourceLength) {
char[] newChars = new char[newLength];
sourceString.getChars(sourceLength - newLength, sourceLength,
newChars, 0);
return newString.equalsIgnoreCase(String.valueOf(newChars));
} else {
return false;
}
}
/**
* 检查字符串长度,如果字符串的长度超过maxLength,就截取前maxLength个字符串并在末尾拼上appendString
*/
public static String checkLength(String string, int maxLength, String appendString) {
if (string.length() > maxLength) {
string = string.substring(0, maxLength);
if (appendString != null) {
string += appendString;
}
}
return string;
}
/**
* 检查字符串长度,如果字符串的长度超过maxLength,就截取前maxLength个字符串并在末尾拼上…
*/
public static String checkLength(String string, int maxLength) {
return checkLength(string, maxLength, "…");
}
}
Toast
import android.content.Context;
import android.widget.Toast;
public class ToastUtil {
private static Context context;
public static Toast toast;
public static void init(Context context) {
ToastUtil.context = context;
}
public static void showToast(int stringId) {
if (toast == null) {
toast = Toast.makeText(context, stringId, Toast.LENGTH_SHORT);
} else {
toast.makeText(context, stringId, Toast.LENGTH_SHORT);
}
toast.setText(stringId);
toast.show();
}
public static void showToast(String text) {
if (toast == null) {
toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
} else {
toast.makeText(context, text, Toast.LENGTH_SHORT);
}
toast.setText(text);
toast.show();
}
}
计算时间
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 用来计算显示的时间是多久之前的!
*/
public class TransitionTime {
private Date endDate;
private static SimpleDateFormat formatBuilder;
public static final int WEEKDAYS = 7;
public static String[] WEEK = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
public TransitionTime(long endTime) {
endDate = new Date(endTime);
}
public long timeMills = 0;
/**
* 时间转换
*
* @param time 时间
* @param timeFormat 时间的格式 eg: yyyy-MM-dd hh:mm:ss
* @return String
*/
public String convert(String time, String timeFormat) {
timeMills = Long.parseLong(time);
Date date = new Date(timeMills);
String strs = "";
try {
SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
strs = sdf.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return strs;
}
/**
* @param startTime 开始的事件
* @return 返回距离发帖时间的时间差
*/
public String twoDateDistance(String startTime) {
if (startTime.equals("")) {
return "";
}
timeMills = Long.parseLong(startTime);
Date startDate = new Date(timeMills);
if (startDate == null || endDate == null) {
return null;
}
long timeLong = endDate.getTime() - startDate.getTime();
if (timeLong <= 0) {
return "刚刚";
} else if (timeLong < 60 * 1000) {
return timeLong / 1000 + "秒前";
} else if (timeLong < 60 * 60 * 1000) {
timeLong = timeLong / 1000 / 60;
return timeLong + "分钟前";
} else if (timeLong < 60 * 60 * 24 * 1000) {
timeLong = timeLong / 60 / 60 / 1000;
return timeLong + "小时前";
} else if (timeLong < 60 * 60 * 24 * 1000 * 7) {
timeLong = timeLong / 1000 / 60 / 60 / 24;
return timeLong + "天前";
} else if (timeLong < 60 * 60 * 24 * 1000 * 7 * 4) {
timeLong = timeLong / 1000 / 60 / 60 / 24 / 7;
return timeLong + "周前";
} else {
timeLong = timeLong / 1000 / 60 / 60 / 24;
return timeLong + "天前";
}
}
/**
* UTM转换成日期描述,如三周前,上午,昨天等
*
* @param milliseconds milliseconds
* @param isShowWeek 是否采用周的形式显示 true 显示为3周前,false 则显示为时间格式mm-dd
* @return 如三周前,上午,昨天等
*/
public static String getTimeDesc(long milliseconds, boolean isShowWeek) {
StringBuffer sb = new StringBuffer();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliseconds);
long hour = calendar.get(Calendar.HOUR_OF_DAY);
calendar.setTimeInMillis(System.currentTimeMillis());
long hourNow = calendar.get(Calendar.HOUR_OF_DAY);
Log.v("---------->---", System.currentTimeMillis() + "----------" + milliseconds);
long datetime = System.currentTimeMillis() - (milliseconds);
long day = (long) Math.floor(datetime / 24 / 60 / 60 / 1000.0f) + (hourNow < hour ? 1 : 0);// 天前
if (day <= 7) {// 一周内
if (day == 0) {// 今天
if (hour <= 4) {
sb.append(" 凌晨 ");
} else if (hour > 4 && hour <= 6) {
sb.append(" 早上 ");
} else if (hour > 6 && hour <= 11) {
sb.append(" 上午 ");
} else if (hour > 11 && hour <= 13) {
sb.append(" 中午 ");
} else if (hour > 13 && hour <= 18) {
sb.append(" 下午 ");
} else if (hour > 18 && hour <= 19) {
sb.append(" 傍晚 ");
} else if (hour > 19 && hour <= 24) {
sb.append(" 晚上 ");
} else {
sb.append("今天 ");
}
} else if (day == 1) {// 昨天
sb.append(" 昨天 ");
} else if (day == 2) {// 前天
sb.append(" 前天 ");
} else {
sb.append(" " + DateToWeek(milliseconds) + " ");
}
} else {// 一周之前
if (isShowWeek) {
sb.append((day % 7 == 0 ? (day / 7) : (day / 7 + 1)) + "周前");
} else {
formatBuilder = new SimpleDateFormat("MM-dd");
String time = formatBuilder.format(milliseconds);
sb.append(time);
}
}
Log.v("sb---", sb.toString() + "");
return sb.toString();
}
/**
* UTM转换成日期描述,如三周前,上午,昨天等
*
* @param milliseconds 时间
* @return UTM转换成日期描述,如三周前,上午,昨天等
*/
public static String getTimeDesc(long milliseconds) {
return getTimeDesc(milliseconds, true);
}
/**
* UTM转换成日期 ,hh:mm
*
* @param milliseconds milliseconds
* @return UTM转换成日期 ,hh:mm
*/
public static String getDisplayTime(long milliseconds) {
formatBuilder = new SimpleDateFormat("HH:mm");
String time = formatBuilder.format(milliseconds);
return time;
}
/**
* UTM转换成带描述的日期
*
* @param milliseconds milliseconds
* @return UTM转换成带描述的日期
*/
public static String getDisplayTimeAndDesc(long milliseconds) {
formatBuilder = new SimpleDateFormat("HH:mm");
String time = formatBuilder.format(milliseconds);
StringBuffer sb = new StringBuffer();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliseconds);
long hour = calendar.get(Calendar.HOUR_OF_DAY);
Log.v("---------->---", System.currentTimeMillis() + "----------" + milliseconds);
long datetime = System.currentTimeMillis() - (milliseconds);
long day = (long) Math.ceil(datetime / 24 / 60 / 60 / 1000.0f);// 天前
Log.v("day---hour---time---", day + "----" + hour + "-----" + time);
if (day <= 7) {// 一周内
if (day == 0) {// 今天
if (hour <= 4) {
sb.append(" 凌晨 " + time);
} else if (hour > 4 && hour <= 6) {
sb.append(" 早上 " + time);
} else if (hour > 6 && hour <= 11) {
sb.append(" 上午 " + time);
} else if (hour > 11 && hour <= 13) {
sb.append(" 中午 " + time);
} else if (hour > 13 && hour <= 18) {
sb.append(" 下午 " + time);
} else if (hour > 18 && hour <= 19) {
sb.append(" 傍晚 " + time);
} else if (hour > 19 && hour <= 24) {
sb.append(" 晚上 " + time);
} else {
sb.append("今天 " + time);
}
} else if (day == 1) {// 昨天
sb.append("昨天 " + time);
} else if (day == 2) {// 前天
sb.append("前天 " + time);
} else {
sb.append(DateToWeek(milliseconds) + time);
}
} else {// 一周之前
sb.append(day % 7 + "周前");
}
Log.v("sb---", sb.toString() + "");
return sb.toString();
}
/**
* 日期变量转成对应的星期字符串
*
* @param milliseconds data
* @return 日期变量转成对应的星期字符串
*/
public static String DateToWeek(long milliseconds) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliseconds);
int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);
if (dayIndex < 1 || dayIndex > WEEKDAYS) {
return null;
}
return WEEK[dayIndex - 1];
}
/**
* 将时间间隔转换成描述性字符串,如2天前,3月1天后等。
*
* @param toDate 相对的日期
* @param isFull 是否全部显示 true 全部显示 false 简单显示
* @return 将时间间隔转换成描述性字符串,如2天前,3月1天后等。
*/
public static String diffDateAsDesc(Date toDate, boolean isFull) {
String diffDesc = "";
String fix = "";
Long diffTime;
Date curDate = new Date();
if (curDate.getTime() > toDate.getTime()) {
diffTime = curDate.getTime() - toDate.getTime();
fix = "前";
} else {
diffTime = toDate.getTime() - curDate.getTime();
fix = "后";
}
//换算成分钟数,防止Int溢出。
diffTime = diffTime / 1000 / 60;
Long year = diffTime / (60 * 24 * 30 * 12);
diffTime = diffTime % (60 * 24 * 30 * 12);
if (year > 0) {
diffDesc = diffDesc + year + "年";
if (!isFull) {
return diffDesc + fix;
}
}
Long month = diffTime / (60 * 24 * 30);
diffTime = diffTime % (60 * 24 * 30);
if (month > 0) {
diffDesc = diffDesc + month + "月";
if (!isFull) {
return diffDesc + fix;
}
}
Long day = diffTime / 60 / 24;
diffTime = diffTime % (60 * 24);
if (day > 0) {
diffDesc = diffDesc + day + "天";
if (!isFull) {
return diffDesc + fix;
}
}
Long hour = diffTime / (60);
diffTime = diffTime % (60);
if (hour > 0) {
diffDesc = diffDesc + hour + "时";
if (!isFull) {
return diffDesc + fix;
}
}
Long minitue = diffTime;
if (minitue > 0) {
diffDesc = diffDesc + minitue + "分";
if (!isFull) {
return diffDesc + fix;
}
}
return diffDesc + fix;
}
}
Zip工具
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
* Java utils 实现的Zip工具
*/
public class ZipUtil {
private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte
private static boolean stopZipFlag;
public static boolean isStopZipFlag() {
return stopZipFlag;
}
public static void setStopZipFlag(boolean stopZipFlag) {
ZipUtil.stopZipFlag = stopZipFlag;
}
/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @param zipListener zipListener
*/
public static void zipFiles(Collection<File> resFileList, File zipFile, ZipListener zipListener) {
ZipOutputStream zipout = null;
try {
zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
if (stopZipFlag) {
break;
}
zipFile(resFile, zipout, "", zipListener);
}
zipout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @param comment 压缩文件的注释
* @param zipListener zipListener
*/
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment, ZipListener zipListener) {
ZipOutputStream zipout = null;
try {
zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
zipFile(resFile, zipout, "", zipListener);
}
zipout.setComment(comment);
zipout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解压缩一个文件
*
* @param zipFile 压缩文件
* @param folderPath 解压缩的目标目录
*/
public static void upZipFile(File zipFile, String folderPath) {
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdirs();
}
ZipFile zf = null;
try {
zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "GB2312");
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[BUFF_SIZE];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解压文件名包含传入文字的文件
*
* @param zipFile 压缩文件
* @param folderPath 目标文件夹
* @param nameContains 传入的文件匹配名
* @return 返回的集合
*/
public static ArrayList<File> upZipSelectedFile(File zipFile, String folderPath,
String nameContains) {
ArrayList<File> fileList = new ArrayList<File>();
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdir();
}
ZipFile zf = null;
try {
zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
if (entry.getName().contains(nameContains)) {
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "GB2312");
// str.getBytes("GB2312"),"8859_1" 输出
// str.getBytes("8859_1"),"GB2312" 输入
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[BUFF_SIZE];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
fileList.add(desFile);
}
}
return fileList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获得压缩文件内文件列表
*
* @param zipFile 压缩文件
* @return 压缩文件内文件名称
*/
public static ArrayList<String> getEntriesNames(File zipFile) {
ArrayList<String> entryNames = new ArrayList<String>();
Enumeration<?> entries = null;
try {
entries = getEntriesEnumeration(zipFile);
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
entryNames.add(new String(getEntryName(entry).getBytes("GB2312"), "8859_1"));
}
return entryNames;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获得压缩文件内压缩文件对象以取得其属性
*
* @param zipFile 压缩文件
* @return 返回一个压缩文件列表
*/
public static Enumeration<?> getEntriesEnumeration(File zipFile) {
ZipFile zf = null;
try {
zf = new ZipFile(zipFile);
} catch (IOException e) {
e.printStackTrace();
}
return zf.entries();
}
/**
* 取得压缩文件对象的注释
*
* @param entry 压缩文件对象
* @return 压缩文件对象的注释
*/
public static String getEntryComment(ZipEntry entry) {
try {
return new String(entry.getComment().getBytes("GB2312"), "8859_1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* 取得压缩文件对象的名称
*
* @param entry 压缩文件对象
* @return 压缩文件对象的名称
*/
public static String getEntryName(ZipEntry entry) {
try {
return new String(entry.getName().getBytes("GB2312"), "8859_1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* 压缩文件
*
* @param resFile 需要压缩的文件(夹)
* @param zipout 压缩的目的文件
* @param rootpath 压缩的文件路径
*/
private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath, ZipListener zipListener) {
try {
rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator)
+ resFile.getName();
rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
int length = fileList.length;
// Log.e("zipprogress", (int)((1 / (float) (length+1))*100)+"%");
zipListener.zipProgress((int) ((1 / (float) (length + 1)) * 100));
for (int i = 0; i < length; i++) {
if (stopZipFlag) {
break;
}
File file = fileList[i];
zipFile(file, zipout, rootpath, zipListener);
// Log.e("zipprogress", (int)(((i+2) / (float) (length+1))*100)+"%");
zipListener.zipProgress((int) (((i + 2) / (float) (length + 1)) * 100));
}
} else {
byte buffer[] = new byte[BUFF_SIZE];
BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile),
BUFF_SIZE);
zipout.putNextEntry(new ZipEntry(rootpath));
int realLength;
while ((realLength = in.read(buffer)) != -1) {
if (stopZipFlag) {
break;
}
zipout.write(buffer, 0, realLength);
}
in.close();
zipout.flush();
zipout.closeEntry();
}
} catch (Exception e) {
}
}
public interface ZipListener {
void zipProgress(int zipProgress);
}
}
参考网址:http://www.open-open.com/lib/view/open1453345797417.html