前言
几乎每个APP都需要图片选择和裁剪功能,因为涉及到相机和存储,所以该功能还是要考虑很多兼容性的,这里封装了一个功能比较齐全的图片选择工具类方便大家使用。效果图如下: 效果图.jpg功能特点
- 支持通过拍照获取图片
- 支持通过相册获取图片
- 支持图片裁剪
- 支持仿IOS底部弹出选择菜单ActionSheet效果
- 支持6.0动态授予权限
- 解决图片有黑边问题
- 解决7.0调用相机报FileUriExposedException的问题
- 解决小米miui系统调用系统裁剪图片功能crash问题
使用
由于进行了封装,所以使用起来非常简单,只需要2步即可。
1. 调用getByCamera()、getByAlbum()可通过拍照或相册获取图片
调用getByCamera()通过拍照获取图片,如下:
SelectPictureUtils.getByCamera(this);
调用getByAlbum()通过相册获取图片,如下:
SelectPictureUtils.getByAlbum(this);
2. 在onActivityResult中调用本工具类的onActivityResult方法处理通过相册或拍照获取的图片
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//裁剪后的图片宽高为400*400,选择图片时的裁剪比例是1:1
Bitmap bitmap = SelectPictureUtils.onActivityResult(this, requestCode, resultCode, data, 480, 480, 1, 1);
if (bitmap != null) {
mIvImage.setImageBitmap(bitmap);
}
}
代码
每个类的注释我都写的很清楚了,所以这里只贴出主要的图片工具类和代码中使用的例子,其他可以到我的Github上查看源码(见文末)。
选择图片工具类
package com.wildma.wildmaselectpicture;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
/**
* Author wildma
* Github https://github.com/wildma
* CreateDate 2018/6/10
* Desc ${选择图片工具类}
* 使用方法:
* 1. 调用getByCamera()、getByAlbum()可通过拍照或相册获取图片
* 2. 在onActivityResult中调用本工具类的onActivityResult方法处理通过相册或拍照获取的图片
*/
public class SelectPictureUtils {
private static final int GET_BY_ALBUM = 0x11;//相册标记
private static final int GET_BY_CAMERA = 0x12;//拍照标记
private static final int CROP = 0x13;//裁剪标记
private static Uri takePictureUri;//拍照图片uri
private static Uri cropPictureTempUri;//裁剪图片uri
/**
* 通过相册获取图片
* @param activity
*/
public static void getByAlbum(Activity activity) {
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
activity.startActivityForResult(intent, GET_BY_ALBUM);
}
/**
* 通过拍照获取图片
* @param activity
*/
public static void getByCamera(Activity activity) {
takePictureUri = createImagePathUri(activity);
if (takePictureUri != null) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, takePictureUri);//输出路径(拍照后的保存路径)
activity.startActivityForResult(i, GET_BY_CAMERA);
} else {
Toast.makeText(activity, "无法保存到相册", Toast.LENGTH_LONG).show();
}
}
/**
* 创建一个图片地址uri,用于保存拍照后的照片
*
* @param activity
* @return 图片的uri
*/
public static Uri createImagePathUri(Activity activity) {
try {
FileUtils.createOrExistsDir(Constant.DIR_ROOT);
StringBuffer buffer = new StringBuffer();
String pathName = buffer.append(Constant.DIR_ROOT).append(Constant.APP_NAME).append(".").append(System.currentTimeMillis()).append(".jpg").toString();
File file = new File(pathName);
if (Build.VERSION.SDK_INT >= 24) { //解决Android 7.0 拍照出现FileUriExposedException的问题
takePictureUri = FileProvider.getUriForFile(activity, Constant.AUTHORITY, file);
} else {
takePictureUri = Uri.fromFile(file);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(activity, "无法保存到相册", Toast.LENGTH_LONG).show();
}
return takePictureUri;
}
/**
* 处理拍照或相册获取的图片,默认大小480*480,比例1:1
* @param activity 上下文
* @param requestCode 请求码
* @param resultCode 结果码
* @param data Intent
* @return
*/
public static Bitmap onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
return onActivityResult(activity, requestCode, resultCode, data, 0, 0, 0, 0);
}
/**
* 处理拍照或相册获取的图片
* @param activity 上下文
* @param requestCode 请求码
* @param resultCode 结果码
* @param data Intent
* @param w 输出宽
* @param h 输出高
* @param aspectX 宽比例
* @param aspectY 高比例
* @return
*/
public static Bitmap onActivityResult(Activity activity, int requestCode, int resultCode, Intent data,
int w, int h, int aspectX, int aspectY) {
Bitmap bm = null;
if (resultCode == Activity.RESULT_OK) {
Uri uri = null;
switch (requestCode) {
case GET_BY_ALBUM:
uri = data.getData();
activity.startActivityForResult(crop(uri, w, h, aspectX, aspectY), CROP);
break;
case GET_BY_CAMERA:
uri = takePictureUri;
activity.startActivityForResult(crop(uri, w, h, aspectX, aspectY), CROP);
break;
case CROP:
bm = dealCrop(activity);
break;
}
}
return bm;
}
/**
* 裁剪,默认裁剪输出480*480,比例1:1
* @param uri 图片的uri
* @return
*/
public static Intent crop(Uri uri) {
return crop(uri, 480, 480, 1, 1);
}
/**
* 裁剪,例如:输出100*100大小的图片,宽高比例是1:1
* @param uri 图片的uri
* @param w 输出宽
* @param h 输出高
* @param aspectX 宽比例
* @param aspectY 高比例
* @return
*/
public static Intent crop(Uri uri, int w, int h, int aspectX, int aspectY) {
if (w == 0 && h == 0) {
w = h = 480;
}
if (aspectX == 0 && aspectY == 0) {
aspectX = aspectY = 1;
}
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("outputX", w);
intent.putExtra("outputY", h);
/*解决图片有黑边问题*/
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
/*解决跳转到裁剪提示“图片加载失败”问题*/
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
/*解决小米miui系统调用系统裁剪图片功能camera.action.CROP后崩溃或重新打开app的问题*/
StringBuffer buffer = new StringBuffer();
String pathName = buffer.append("file:///").append(FileUtils.getRootPath()).append(File.separator).append(Constant.APP_NAME).append(".temp.jpg").toString();
cropPictureTempUri = Uri.parse(pathName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropPictureTempUri);//输出路径(裁剪后的保存路径)
// 输出格式
intent.putExtra("outputFormat", "JPEG");
// 不启用人脸识别
intent.putExtra("noFaceDetection", true);
//是否将数据保留在Bitmap中返回
intent.putExtra("return-data", false);
return intent;
}
/**
* 处理裁剪,获取裁剪后的图片
* @param context 上下文
* @return
*/
public static Bitmap dealCrop(Context context) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(cropPictureTempUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap;
}
}
MainActivity
package com.wildma.wildmaselectpicture;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();
private ImageView mIvImage;
private final int PERMISSION_CODE_FIRST = 1;//权限请求码
private final int PERMISSION_CODE_SECOND = 2;//权限请求码
private SelectPictureDialog mSelectPictureDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIvImage = (ImageView) findViewById(R.id.iv_image);
//请求应用需要的所有权限
boolean checkPermissionFirst = PermissionUtils.checkPermissionFirst(this, PERMISSION_CODE_FIRST,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA});
}
/**
* 选择图片按钮点击事件
*
* @param view
*/
public void selectPicture(View view) {
boolean checkPermissionSecond = PermissionUtils.checkPermissionSecond(this, PERMISSION_CODE_SECOND,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA});
if (checkPermissionSecond) {
selectPicture();
} else {
Toast.makeText(getApplicationContext(), "请手动打开该应用需要的权限", Toast.LENGTH_SHORT).show();
}
}
/**
* 处理请求权限的响应
*
* @param requestCode 请求码
* @param permissions 权限数组
* @param grantResults 请求权限结果数组
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean isPermissions = true;
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
isPermissions = false;
}
}
if (isPermissions) {
Log.d(TAG, "onRequestPermissionsResult: " + "允许所有权限");
if (requestCode == PERMISSION_CODE_SECOND) {
selectPicture();
}
} else {
Log.d(TAG, "onRequestPermissionsResult: " + "有权限不允许");
}
}
/**
* 选择图片
*/
public void selectPicture() {
mSelectPictureDialog = new SelectPictureDialog(this, R.style.ActionSheetDialogStyle);
mSelectPictureDialog.setOnItemClickListener(new SelectPictureDialog.OnItemClickListener() {
@Override
public void onItemClick(int type) {
if (type == Constant.CAMERA) {
SelectPictureUtils.getByCamera(MainActivity.this);
} else if (type == Constant.ALBUM) {
SelectPictureUtils.getByAlbum(MainActivity.this);
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//裁剪后的图片宽高为400*400,选择图片时的裁剪比例是1:1
Bitmap bitmap = SelectPictureUtils.onActivityResult(this, requestCode, resultCode, data, 480, 480, 1, 1);
if (bitmap != null) {
mIvImage.setImageBitmap(bitmap);
}
}
}
github地址:WildmaSelectPicture
参考资料:
本文Demo主要是在如下工具类的基础上解决了它存在的问题并且新增了其他功能。
网友评论
非常感谢~~~