1.首先导入依赖
implementation 'com.github.stf-android:MultiImageSelector:1.0.1'
2.点击图片调取MultiImageSelectorActivity
private static final int REQUEST_IMAGEHEME = 4;
更换的图片.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//最简单的调取相机相册动态权限,不加任何逻辑的
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA,Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Intent intent = new Intent(getActivity(), MultiImageSelectorActivity.class);
intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
// 最大图片选择数量
intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
// 设置模式 (支持 单选/MultiImageSelectorActivity.MODE_SINGLE 或者 多选/MultiImageSelectorActivity.MODE_MULTI)
intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
startActivityForResult(intent, REQUEST_IMAGEHEME);
}
});
3.接受返回数据就行
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGEHEME) {
if (resultCode == RESULT_OK) {
//返回的是个集合,多图选择的话遍历出来就行,如果就一张如跟换头像什么的就去下标就行
List<String> list = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
String s = list.get(0);
//将取到的地址转成bitmap
Bitmap compressedBitmap = BitmapUtils.getCompressedBitmap(s);
更换的图片.setImageBitmap(compressedBitmap);
//如果有接口需求上传base64,在转成就行
base64 = bitmapToBase64(compressedBitmap);
Log.e("base64++++", base64 + "");
}
}
}
4.BitmapUtils工具类转成为bitmap
public class BitmapUtils {
public static Bitmap getCompressedBitmap(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = computeSampleSize(options, -1,480*800);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 :(int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}
5.bitmapToBase64工具类转为base64
public String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.flush();
baos.close();
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}