前言
很多的東西都是在閱讀書(shū)或者閱讀他人文章或者自己開(kāi)發(fā)過(guò)程中覺(jué)得很有意義的東西分享給大家冲秽,還有就是幫助我日后查看相關(guān)東西。
今日分享
1矩父、簡(jiǎn)單封裝Toast
2锉桑、網(wǎng)絡(luò)判斷工具類(lèi)
3、Image工具類(lèi)
直接上代碼https://github.com/xiaogoudandan/WilliamApp/tree/master/base/src/main/java/com/william_zhang/utils
簡(jiǎn)單封裝Toast
在日常中窍株,用戶操作或者是加載結(jié)果等其他事件都需要界面上顯示提示語(yǔ)句民轴,現(xiàn)在比較常用的提示是Toast
或者SnackBar
,后者是在Design庫(kù)中的,今天簡(jiǎn)單封裝的還是前者Toast
球订。
有興趣可以去研究研究SnackBar
后裸,今天我就不分享了。
常見(jiàn)的問(wèn)題:
多個(gè)Toast冒滩,需要等待前面的Toast
結(jié)束后微驶,后面的Toast
才會(huì)顯示。
當(dāng)前需要的時(shí)候就顯示一個(gè)开睡,如果另外一個(gè)Toast
顯示了因苹,立馬替換掉前一個(gè)。
一般的做法就是全部的Toast
都使用同一個(gè)對(duì)象篇恒。(靜態(tài)對(duì)象)
直接上代碼:
public class WilliamToast {
private static Context mContext;
private static Toast mToast;
/**
* applicationContext 綁定給Toast
* 建議在application中調(diào)用init()扶檐;
*
* @param context
*/
public static void init(@NonNull Context context) {
mContext = context;
}
public static void show(String test) {
if (mContext == null) {
throw new NullPointerException();
}
if (mToast == null) {
mToast = Toast.makeText(mContext, test, Toast.LENGTH_SHORT);
} else {
mToast.setText(test);//立馬替換
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
public static void show(int id) {
if (mContext == null) {
throw new NullPointerException();
}
if (mToast == null) {
mToast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
} else {
mToast.setText(id);//立馬替換
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
public static void showLong(int id) {
if (mContext == null) {
throw new NullPointerException();
}
if (mToast == null) {
mToast = Toast.makeText(mContext, id, Toast.LENGTH_LONG);
} else {
mToast.setText(id);//立馬替換
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
public static void showLong(String test) {
if (mContext == null) {
throw new NullPointerException();
}
if (mToast == null) {
mToast = Toast.makeText(mContext, test, Toast.LENGTH_LONG);
} else {
mToast.setText(test);//立馬替換
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
public static void cancelToast() {
if (mToast != null) {
mToast.cancel();
}
}
}
提示一下
別忘記在Application中初始化一下,WilliamToast.init();
胁艰。
網(wǎng)絡(luò)判斷工具類(lèi)
網(wǎng)絡(luò)的判斷在日常開(kāi)發(fā)中也是比較常見(jiàn)的款筑,我一般都是配合廣播監(jiān)聽(tīng)網(wǎng)絡(luò)變化來(lái)動(dòng)態(tài)改變目前狀態(tài)智蝠。
工具類(lèi)的功能:
1、當(dāng)前網(wǎng)絡(luò)是否可以用(wifi可用||數(shù)據(jù)可用)
isNetWorkAvailable(context)
2奈梳、當(dāng)前連接的類(lèi)型
getConnectedType(context)
3寻咒、當(dāng)前是否wifi
isWifiConnected(context)
4、當(dāng)期是否數(shù)據(jù)流量
isMobileConnected(context)
上代碼:
public class NetWorkUtil {
public static final int INVALID_NETWORK_TYPE = -1;//不可用
public static final int MOBILE_NETWORK_TYPE = 0;//數(shù)據(jù)流量
public static final int WIFI_NETWORK_TYPE = 1;//wifi
/**
* 是否wifi或者數(shù)據(jù)可用
* @param context
* @return
*/
public static boolean isNetWorkAvailable(@NonNull Context context) {
boolean isWifi = false;
boolean isMoblie = false;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
for (NetworkInfo info : networkInfos) {
if (info.getTypeName().equals("WIFI")) {
if (info.isConnected() && info.isAvailable()) {
isWifi = true;
}
}
if (info.getTypeName().equals("MOBILE")) {
if (info.isConnected() && info.isAvailable()) {
isMoblie = true;
}
}
}
return isMoblie || isWifi;
}
/**
* 當(dāng)前網(wǎng)絡(luò)類(lèi)型
* @param context
* @return
*/
public static int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
}
/**
* 當(dāng)前連接是否是wifi
* @param context
* @return
*/
public static boolean isWifiConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.isAvailable()&&mWiFiNetworkInfo.isConnected();
}
}
return false;
}
/**
* 當(dāng)前連接是否是數(shù)據(jù)
* @param context
* @return
*/
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable()&&mMobileNetworkInfo.isConnected();
}
}
return false;
}
}
Image工具類(lèi)
圖片很容易產(chǎn)生OOM(什么是內(nèi)存泄漏颈嚼,自行百度)毛秘,或者大小不合適等,就需要對(duì)圖片進(jìn)行處理阻课〗行或者是對(duì)圖片進(jìn)行保存等和bitmap有關(guān)的一系列操作。
壓縮圖片限煞,長(zhǎng)寬改變
1抹恳、根據(jù)文件路徑壓縮
Bitmap scaleCompress(String filePath, int width, int height)
2、根據(jù)bitmap進(jìn)行壓縮
Bitmap scaleCompress(Bitmap bitmap, int width, int height)
3署驻、根據(jù)資源壓縮
Bitmap scaleCompress(Resources resources, int resId, int width, int heigth)
質(zhì)量壓縮
4奋献、根據(jù)可接受的最大存儲(chǔ)空間壓縮
Bitmap qualityCompress(Bitmap bitmap, int max)
5、將圖片保存到文件中去
boolean saveBitmaoToFile(String file, Bitmap bitmap)
6旺上、將文件轉(zhuǎn)換成bitmap
Bitmap fileToBitmap(String filePath)
7瓶蚂、資源文件轉(zhuǎn)為bitmap
Bitmap resourcesToBitmap(Context context, @DrawableRes int id)
8、根據(jù)uri轉(zhuǎn)為bitmap
Bitmap decodeUriAsBitmap(Context context, Uri uri)
9宣吱、圖片進(jìn)行高斯模糊處理
方法1:Bitmap blurBitmap(Bitmap sentBitmap, int radius, boolean canReuseInBitmap)
方法2:Bitmap blurBitmap(Context context, Bitmap image, float blurRadius)
10窃这、調(diào)用手機(jī)攝像頭拍照 在onActivityResult中獲得圖片uri
void getImageFromCamera(Activity activity, Uri uri)
11、從相冊(cè)中獲得圖片 在onActivityResult中獲得圖片
void getImageFromAlbums(Activity activity)
12征候、拍照的照片進(jìn)行裁剪
cameraImageCrop(Context context, String providerAuthority, File file)
13杭攻、相冊(cè)的照片進(jìn)行裁剪
albumImageCrop(Context context, File file)
上代碼:有點(diǎn)多,別激動(dòng)
public class ImageUtil {
/**
* @param resources 根據(jù)資源壓縮
* @param resId
* @param width
* @param heigth
* @return
*/
public static Bitmap scaleCompress(Resources resources, int resId, int width, int heigth) {
BitmapFactory.Options options = new BitmapFactory.Options();
//如果該 值設(shè)為true那么將不返回實(shí)際的bitmap
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, resId, options);
//Options中有個(gè)屬性inSampleSize疤坝。我們可以充分利用它兆解,實(shí)現(xiàn)縮放。
// 如果被設(shè)置為一個(gè)值> 1,要求解碼器解碼出原始圖像的一個(gè)子樣本,返回一個(gè)較小的bitmap,以節(jié)省存儲(chǔ)空間跑揉。
//例如,inSampleSize = = 2锅睛,則取出的縮略圖的寬和高都是原始圖片的1/2,圖片大小就為原始大小的1/4畔裕。
options.inSampleSize = getInSampleSize(options, width, heigth);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(resources, resId, options);
}
/**
* @param filePath 根據(jù)文件路徑壓縮
* @param width
* @param height
* @return
*/
public static Bitmap scaleCompress(String filePath, int width, int height) {
BitmapFactory.Options options = new BitmapFactory.Options();
//如果該 值設(shè)為true那么將不返回實(shí)際的bitmap
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath);
//Options中有個(gè)屬性inSampleSize衣撬。我們可以充分利用它乖订,實(shí)現(xiàn)縮放扮饶。
// 如果被設(shè)置為一個(gè)值> 1,要求解碼器解碼出原始圖像的一個(gè)子樣本,返回一個(gè)較小的bitmap,以節(jié)省存儲(chǔ)空間。
//例如,inSampleSize = = 2乍构,則取出的縮略圖的寬和高都是原始圖片的1/2甜无,圖片大小就為原始大小的1/4扛点。
options.inSampleSize = getInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
/**
* @param bitmap 根據(jù)bitmap進(jìn)行壓縮
* @param width
* @param height
* @return
*/
public static Bitmap scaleCompress(Bitmap bitmap, int width, int height) {
//轉(zhuǎn)化成字節(jié)
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.reset();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
options.inSampleSize = getInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
}
/**
* @param options
* @param width 壓縮寬
* @param height 壓縮高
* @return inSampleSize
*/
public static int getInSampleSize(BitmapFactory.Options options, int width, int height) {
//原圖的長(zhǎng)寬以及inSampleSize
int mWidth = options.outWidth;
int mHeight = options.outHeight;
//小于1都是原圖的大小
int inSampleSize = 1;
if (mWidth > width || mHeight > height) {
//正數(shù):Math.round(11.5) = 12
//負(fù)數(shù):Math.round(-11.5) = -11
int widthScale = Math.round((float) mWidth / (float) width);
int heightScale = Math.round((float) mHeight / (float) height);
inSampleSize = widthScale > heightScale ? heightScale : widthScale;
}
return inSampleSize;
}
/**
* 質(zhì)量壓縮,改變的是存儲(chǔ)空間岂丘,不改變大小
*
* @param bitmap
* @param max 可以接受的最大的大小
* @return
*/
public static Bitmap qualityCompress(Bitmap bitmap, int max) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream);
//不滿足大小要求繼續(xù)壓縮
while ((byteArrayOutputStream.toByteArray().length / 1024) > max) {
byteArrayOutputStream.reset();
if (quality > 10) {
quality -= 10;
} else {
quality -= 1;
}
if (quality == 0) {
break;
}
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream);
}
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
return BitmapFactory.decodeStream(byteArrayInputStream, null, null);
}
/**
* 將圖片保存到文件中去
*
* @param file
* @param bitmap
* @return
*/
public static boolean saveBitmaoToFile(String file, Bitmap bitmap) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
//壓縮質(zhì)量為85%
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fileOutputStream);
fileOutputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* 將文件轉(zhuǎn)換成bitmap
*
* @param filePath 文件名
* @return Bitmap
*/
public static Bitmap fileToBitmap(String filePath) {
return BitmapFactory.decodeFile(filePath);
}
/**
* 資源文件轉(zhuǎn)為bitmap
*
* @param context 上下文
* @param id 資源id
* @return bitmap
*/
public static Bitmap resourcesToBitmap(Context context, @DrawableRes int id) {
Resources resources = context.getResources();
return BitmapFactory.decodeResource(resources, id);
}
/**
* 根據(jù)uri轉(zhuǎn)為bitmap
*
* @param context context
* @param uri uri
* @return btimap
*/
public static Bitmap decodeUriAsBitmap(Context context, Uri uri) {
if (context == null || uri == null) return null;
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}
/**
* 圖片直接處理實(shí)現(xiàn)模糊
*
* @param sentBitmap
* @param radius
* @param canReuseInBitmap
* @return
*/
public static Bitmap blurBitmap(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) {
Bitmap bitmap;
if (canReuseInBitmap) {
bitmap = sentBitmap;
} else {
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];
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;
}
}
bitmap.setPixels(pix, 0, w, 0, 0, w, h);
return (bitmap);
}
// 圖片縮放比例(即模糊度)
private static final float BITMAP_SCALE = 0.4f;
/**
* 使用android api實(shí)現(xiàn)模糊
*
* @param context 上下文
* @param image 待處理文件
* @param blurRadius 最大25f
* @return
*/
public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
// 計(jì)算圖片縮小后的長(zhǎng)寬
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
// 將縮小后的圖片做為預(yù)渲染的圖片
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
// 創(chuàng)建一張渲染后的輸出圖片
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
// 創(chuàng)建RenderScript內(nèi)核對(duì)象
RenderScript rs = RenderScript.create(context);
// 創(chuàng)建一個(gè)模糊效果的RenderScript的工具對(duì)象
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// 由于RenderScript并沒(méi)有使用VM來(lái)分配內(nèi)存,所以需要使用Allocation類(lèi)來(lái)創(chuàng)建和分配內(nèi)存空間
// 創(chuàng)建Allocation對(duì)象的時(shí)候其實(shí)內(nèi)存是空的,需要使用copyTo()將數(shù)據(jù)填充進(jìn)去
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
// 設(shè)置渲染的模糊程度, 25f是最大模糊度
blurScript.setRadius(blurRadius);
// 設(shè)置blurScript對(duì)象的輸入內(nèi)存
blurScript.setInput(tmpIn);
// 將輸出數(shù)據(jù)保存到輸出內(nèi)存中
blurScript.forEach(tmpOut);
// 將數(shù)據(jù)填充到Allocation中
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
public static final int REQ_PHOTO_CAMERA = 110; // 拍照
public static final int REQ_PHOTO_ALBUM = 120; // 相冊(cè)
public static final int REQ_PHOTO_CROP = 130; // 裁剪
/**
* 調(diào)用手機(jī)攝像頭拍照 在onActivityResult中獲得圖片uri
*
* @param activity
* @param uri
*/
public static void getImageFromCamera(Activity activity, Uri uri) {
if (Build.VERSION.SDK_INT < 24) {
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
activity.startActivityForResult(openCameraIntent, REQ_PHOTO_CAMERA);
} else {//7.0系統(tǒng)
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加這一句表示對(duì)目標(biāo)應(yīng)用臨時(shí)授權(quán)該Uri所代表的文件
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設(shè)置Action為拍照
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//將拍取的照片保存到指定URI
activity.startActivityForResult(intent, REQ_PHOTO_CAMERA);
}
}
/**
* 從相冊(cè)中獲得圖片 在onActivityResult中獲得圖片
*
* @param activity
*/
public static void getImageFromAlbums(Activity activity) {
Intent openAlbumIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
openAlbumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
activity.startActivityForResult(openAlbumIntent, REQ_PHOTO_ALBUM);
}
/**
* 拍照的照片進(jìn)行裁剪
*
* @param context
* @param providerAuthority
* @param file
*/
public static void cameraImageCrop(Context context, String providerAuthority, File file) {
if (Build.VERSION.SDK_INT < 24) {
startImageCrop(context, file);
} else {
//7.0系統(tǒng)陵究,通過(guò)FileProvider創(chuàng)建一個(gè)content類(lèi)型的Uri
Uri imageUri = FileProvider.getUriForFile(context, providerAuthority, file);
startImageCropAPI24(context, file);
}
}
/**
* 相冊(cè)的照片進(jìn)行裁剪
*
* @param context
* @param file
*/
public static void albumImageCrop(Context context, File file) {
if (Build.VERSION.SDK_INT < 24) {
startImageCrop(context, file);
} else {
//7.0系統(tǒng),通過(guò)FileProvider創(chuàng)建一個(gè)content類(lèi)型的Uri
startImageCropAPI24(context, file);
}
}
/**
* 低于sdk24
*
* @param context
* @param file
*/
private static void startImageCrop(Context context, File file) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(file), "image/*");
// crop為true是設(shè)置在開(kāi)啟的intent中設(shè)置顯示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是寬高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁圖片的寬高
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("ic_return-data", true);
intent.putExtra("noFaceDetection", true);
((Activity) context).startActivityForResult(intent, REQ_PHOTO_CROP);
}
/**
* sdk 24之后
*
* @param context
* @param file
*/
private static void startImageCropAPI24(Context context, File file) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(Uri.fromFile(file), "image/*");
// crop為true是設(shè)置在開(kāi)啟的intent中設(shè)置顯示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是寬高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁圖片的寬高
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("ic_return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
((Activity) context).startActivityForResult(intent, REQ_PHOTO_CROP);
}
}