在AS中导入:
compile 'cn.finalteam:galleryfinal:1.4.6'
compile 'com.github.bumptech.glide:glide:3.5.2'
在MyApplicition中初始化配置:
ThemeConfig theme = new ThemeConfig.Builder()
.setTitleBarBgColor(Color.parseColor("#ff0000"))
.setTitleBarTextColor(Color.WHITE)
.setTitleBarIconColor(Color.WHITE)
.setCheckSelectedColor(Color.parseColor("#639ddb"))
.setFabNornalColor(Color.parseColor("#639ddb"))
.setFabPressedColor(Color.parseColor("#639ddb"))
.setIconFab(R.mipmap.nim_ic_trans_fail)
.setIconRotate(R.mipmap.nim_ic_trans_fail)
.build();
//配置功能
FunctionConfig functionConfig = new FunctionConfig.Builder()
.setEnableEdit(false)
.setEnableCrop(false)
.setEnableRotate(false)
.setEnableCamera(true)
.setEnablePreview(true)
.build();
//配置imageloader
GlideImageLoader imageLoader = new GlideImageLoader();
CoreConfig coreConfig = new CoreConfig.Builder(getApplicationContext(), imageLoader, theme)
.setFunctionConfig(functionConfig)
.setEditPhotoCacheFolder(new File(Environment.getExternalStorageDirectory().getPath() + File.separator + getPackageName()))
.setTakePhotoFolder(new File(Environment.getExternalStorageDirectory().getPath() + File.separator + getPackageName()))
.setNoAnimcation(true)
.build();
GalleryFinal.init(coreConfig);
这个是GilderImageLoader类:
public class GlideImageLoader implements cn.finalteam.galleryfinal.ImageLoader {
@Override
public void displayImage(Activity activity, String path, final GFImageView imageView, Drawable defaultDrawable, int width, int height) {
Glide.with(activity)
.load("file://" + path)
.override(width, height)
.diskCacheStrategy(DiskCacheStrategy.NONE) //不缓存到SD卡
.skipMemoryCache(true)
//.centerCrop()
.into(new ImageViewTarget
@Override
protected void setResource(GlideDrawable resource) {
imageView.setImageDrawable(resource);
}
@Override
public void setRequest(Request request) {
imageView.setTag(R.id.adapter_item_tag_key, request);
}
@Override
public Request getRequest() {
return (Request) imageView.getTag(R.id.adapter_item_tag_key);
}
});
}
@Override
public void clearMemoryCache() {
}
}
在values文件下新建ids.xml文件
使用时根据需求配置functionConfig
FunctionConfig functionConfig = new FunctionConfig.Builder()
.setMutiSelectMaxSize(1)
.setEnableEdit(false)
.setEnableCrop(false)
.setCropWidth(getResources().getDisplayMetrics().widthPixels)
.setCropHeight(getResources().getDisplayMetrics().widthPixels)
.setEnableRotate(false)
.setEnableCamera(false)
.setEnablePreview(false)
.setCropSquare(false)
.setForceCrop(false)
.setForceCropEdit(false)
.build();
GalleryFinal.openGalleryMuti(1006, functionConfig, new GalleryFinal.OnHanlderResultCallback() {
@Override
public void onHanlderSuccess(int reqeustCode, List
File file = new File(resultList.get(0).getPhotoPath());
Bitmap bitmap1 = BitmapFactory.decodeFile(resultList.get(0).getPhotoPath());
Log.d("TAG0", file.length() + "");
//如果图片大小大于200kb 进行压缩
if (file.length() > 200 * 1024) {
BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeFile(resultList.get(0).getPhotoPath(), option);
final int height = option.outHeight;
final int width = option.outWidth;
int ratio = 1;
if (height > 1280 || width > 300) {
final int heightRatio = Math.round((float) height / (float) 1280);
final int widthRation = Math.round((float) width / (float) 720);
ratio = heightRatio > widthRation ? heightRatio : widthRation;
}
option.inSampleSize = ratio;
option.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(resultList.get(0).getPhotoPath(), option);
int degree = 0;
//图片属性
try {
ExifInterface exifInterface = new ExifInterface(new File(resultList.get(0).getPhotoPath()).getAbsolutePath());
int oreentaton = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (oreentaton) {
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();
}
if (degree == 0) {
int sourceWidth = bitmap.getWidth();
int sourceHeight = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(90);
// 当进行的不只是平移操作的时候,最后的参数为true,可以进行滤波处理,有助于改善新图像质量
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, sourceWidth, sourceHeight, matrix, true);
File resultFile = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + getPackageName(), UUID.randomUUID() + ".png");
try {
FileOutputStream fos = new FileOutputStream(resultFile);
newBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Log.d("TAG0", resultFile.length() + "");
imag.setImageBitmap(newBitmap);
}
}
}
@Override
public void onHanlderFailure(int requestCode, String errorMsg) {
}
});
}