最近看了AndroidUtilCode(https://github.com/Blankj/AndroidUtilCode) 这个有名的开源工具,记录下自己的学习总结
bitmap工具类
public class BitmapUtil {
private static volatile BitmapUtil instance;
private BitmapUtil() {
}
public static BitmapUtil getInstance() {
if (instance == null) {
synchronized (BitmapUtil.class) {
if (instance == null) {
instance = new BitmapUtil();
}
}
}
return instance;
}
/**
* 读取资源文件夹下图片
*
* @param res getResources
* @param id 文件id
* @return bitmap
*/
public Bitmap getBitmapResources(Resources res, int id) {
TypedValue value = new TypedValue();
InputStream is = res.openRawResource(id, value);
return getBitmap(is);
}
/**
* 读取资源文件夹下图片的缩略图
*
* @param res getResources
* @param id 文件id
* @param maxWidth 最大宽度
* @param maxHeight 最大高度
* @param isRGB 是否使用RGB_565压缩图片
* @return bitmap
*/
public Bitmap getBitmapResources(Resources res, int id, int maxWidth, int maxHeight, boolean isRGB) {
TypedValue value = new TypedValue();
InputStream is = res.openRawResource(id, value);
byte[] bytes = input2Byte(is);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
if (isRGB) {
options.inPreferredConfig = Bitmap.Config.RGB_565;
}
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
}
/**
* 获取ByteArrayOutputStream字节数组
*
* @param is InputStream
* @return 字节数组
*/
private byte[] input2Byte(InputStream is) {
if (is == null) return null;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len;
while ((len = is.read(b, 0, 1024)) != -1) {
os.write(b, 0, len);
}
return os.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* decodeFile4.4以上直接使用的FileInputStream,需要多次读取
* decodeStream可以使用BufferedInputStream提高读写性能
*
* @param is io流
* @return bitmap
*/
private Bitmap getBitmap(InputStream is) {
return BitmapFactory.decodeStream(new BufferedInputStream(is));
}
/**
* 读取sd卡图片
*
* @param file 图片文件
* @return 高清bitmap
*/
public Bitmap getBitmapFile(File file) {
try {
return getBitmap(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 读取sd卡图片
*
* @param path 图片文件路径
* @param fileName 文件名字
* @return 高清bitmap
*/
public Bitmap getBitmapFile(String path, String fileName) {
return getBitmapFile(new File(path, fileName));
}
/**
* 读取sd卡图片的缩略图
*
* @param path 图片文件路径
* @param fileName 文件名字
* @param maxWidth 最大宽度 建议先获取控件大小
* @param maxHeight 最大高度 建议先获取控件大小
* @param isRGB 是否使用RGB_565压缩图片
* @return bitmap
*/
public Bitmap getBitmapFile(String path, String fileName, int maxWidth, int maxHeight, boolean isRGB) {
return getBitmapFile(new File(path, fileName), maxWidth, maxHeight, isRGB);
}
/**
* 读取sd卡图片的缩略图
*
* @param file 图片文件
* @param maxWidth 最大宽度
* @param maxHeight 最大高度
* @param isRGB 是否使用RGB_565压缩图片
* @return bitmap
*/
public Bitmap getBitmapFile(File file, int maxWidth, int maxHeight, boolean isRGB) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
if (isRGB) {
options.inPreferredConfig = Bitmap.Config.RGB_565;
}
options.inJustDecodeBounds = false;
try {
return BitmapFactory.decodeStream(new BufferedInputStream(new FileInputStream(file)), null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取缩放比例
*
* @param options BitmapFactory.Options
* @param maxWidth 最大宽度
* @param maxHeight 最大高度
* @return inSampleSize
*/
private int calculateInSampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight) {
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = 1;
while (height > maxHeight || width > maxWidth) {
height >>= 1;
width >>= 1;
inSampleSize <<= 1;
}
return inSampleSize;
}
}
安卓系统设备相关类
/**
* 设备相关
*/
object DeviceUtils {
/**
* 查询是否有root权限
*/
fun isDeviceRooted(): Boolean {
val su = "su"
val locations = arrayOf(
"/system/bin/",
"/system/xbin/",
"/sbin/",
"/system/sd/xbin/",
"/system/bin/failsafe/",
"/data/local/xbin/",
"/data/local/bin/",
"/data/local/",
"/system/sbin/",
"/usr/bin/",
"/vendor/bin/"
)
for (location in locations) {
if (File(location + su).exists()) {
return true
}
}
return false
}
/**
* 查询是否启用ADB。
*/
fun isAdbEnabled(): Boolean {
return Settings.Secure.getInt(
OKHttpUtil.INSTANCE.application.contentResolver,
Settings.Global.ADB_ENABLED, 0
) > 0
}
/**
* 获取设备系统版本名字
*/
fun getSDKVersionName(): String {
return android.os.Build.VERSION.RELEASE
}
/**
* 获取设备系统版本号
*/
fun getSDKVersionCode(): Int {
return android.os.Build.VERSION.SDK_INT
}
/**
* 获取设备AndroidID
*/
@SuppressLint("HardwareIds")
fun getAndroidID(): String {
return Settings.Secure.getString(OKHttpUtil.INSTANCE.application.contentResolver, Settings.Secure.ANDROID_ID)
}
/**
* 获取设备MAC地址
* 需要<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
* <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
*/
@SuppressLint("HardwareIds")
fun getMacAddress(): String? {
val macAddress: String?
val wifiManager = OKHttpUtil.INSTANCE.application.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
val info = wifiManager.connectionInfo
if (!wifiManager.isWifiEnabled) {
//必须先打开,才能获取到MAC地址
wifiManager.isWifiEnabled = true
wifiManager.isWifiEnabled = false
}
macAddress = info?.macAddress
return macAddress
}
/**
* 获取设备厂商名字
*/
fun getManufacturer(): String {
return Build.MANUFACTURER
}
/**
* 获取设备型号
*/
fun getModel(): String {
return Build.MODEL
}
}
剪贴板工具类
object ClipboardUtil {
lateinit var clipboardManager :ClipboardManager
private lateinit var application:Application
fun init(application:Application){
this.application = application
clipboardManager = application.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
}
/**
* 复制文本到剪贴板
* 参数1:需要复制的文本
*/
fun copyText(text: CharSequence) {
//参数一:标签,可为空,参数二:要复制到剪贴板的文本
clipboardManager.primaryClip = ClipData.newPlainText("text", text)
}
/**
* 获取剪贴板的文本
*/
fun getText(): CharSequence {
val clip = clipboardManager.primaryClip
if (clip != null && clip.itemCount > 0) {
val item = clip.getItemAt(0)
return item.coerceToText(application)
}
return ""
}
/**
* 复制uri到剪贴板
* 参数1:需要复制的uri ,uri = URL("").toURI()
*/
fun copyUri(uri: Uri) {
clipboardManager.primaryClip = ClipData.newUri(application.contentResolver, "uri", uri)
}
/**
* 获取剪贴板的uri
*/
fun getUri(): Uri? {
val clip = clipboardManager.primaryClip
if (clip != null && clip.itemCount > 0) {
return clip.getItemAt(0).uri
}
return null
}
/**
* 复制意图到剪贴板
* 参数intent 意图
*/
fun copyIntent(intent: Intent) {
clipboardManager.primaryClip = ClipData.newIntent("intent", intent)
}
/**
* 获取剪贴板的意图
*/
fun getIntent(): Intent? {
val clip = clipboardManager.primaryClip
return if (clip != null && clip.itemCount > 0) {
clip.getItemAt(0).intent
} else null
}
}