添加权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
6.0申请动态权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA))
ToastUtils.showLong(this, "您已经拒绝过权限访问");
String[] permissions = new String[]{Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSIONS);
}
}
Android7.0开始传递file://URI会触发FileUriExposedException,使用FileProvider获取Uri就会将以前的file:// URI准换成content:// URI,实现一种安全的应用间数据访问
在AndroidManifest.xml中添加如下代码
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
在res目录下新建一个xml文件夹,并且新建一个file_paths的xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path
name="camera_photos"
path="" />
</paths>
</resources>
调用相机拍照
private Uri imageUri;
private static final int CODE_CAMERA_REQUEST = 1000;
private File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/photo.jpg");
private void camera(){
//图片存储地址
imageUri = Uri.fromFile(fileUri);
//判断当前手机版本7.0存储地址
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
imageUri = FileProvider.getUriForFile(AccountSettingsActivity.this, "com.example.yinyuetai.fileprovider", fileUri);
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
activity.startActivityForResult(intent, CODE_CAMERA_REQUEST );
}
拍照回调处理
private Uri cropImageUri;
private static final int OUTPUT_X = 480;
private static final int OUTPUT_Y = 480;
private static final int CODE_RESULT_REQUEST = 1001;
private File fileCropUri = new File(Environment.getExternalStorageDirectory().getPath() + "/crop_photo.jpg");
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case CODE_CAMERA_REQUEST://访问拍照回调
//裁剪后的图片地址
cropImageUri = Uri.fromFile(fileCropUri);
//裁剪图片
cropImageUri(this, imageUri, cropImageUri, 1, 1, OUTPUT_X, OUTPUT_Y, CODE_RESULT_REQUEST);
break;
}
}
裁剪图片
/**
* 图片裁剪
*
* @param activity 当前activity
* @param orgUri 裁剪图片的Uri
* @param desUri 裁剪图片后的Uri
* @param aspectX X方向的比例
* @param aspectY Y方向的比例
* @param width 裁剪图片的宽度
* @param height 裁剪图片的高度
* @param requestCode 裁剪图片的返回码
*/
public void cropImageUri(Activity activity, Uri orgUri, Uri desUri, int aspectX, int aspectY, int width, int height, int requestCode) {
Intent intent = new Intent("com.android.camera.action.CROP");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(orgUri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("outputX", width);
intent.putExtra("outputY", height);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, desUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
activity.startActivityForResult(intent, requestCode);
}
裁剪图片回调处理
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case CODE_RESULT_REQUEST:
Bitmap bitmap = getBitmapFromUri(cropImageUri, this);
break;
}
}
获取裁剪后的图片
public Bitmap getBitmapFromUri(Uri uri, Context context) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
return bitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
调用相册
private static final int CODE_GALLERY_REQUEST = 1002;
public void album() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
activity.startActivityForResult(intent, CODE_GALLERY_REQUEST );
}
调用相册回调处理
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case CODE_GALLERY_REQUEST:
//裁剪后的图片地址
cropImageUri = Uri.fromFile(fileCropUri);
//获取选中图片的Uri
Uri uri = Uri.parse(getPath(this, data.getData()));
//判断当前手机版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
uri = FileProvider.getUriForFile(this, "com.example.yinyuetai.fileprovider", new File(uri.getPath()));
//裁剪图片
cropImageUri(this, uri, cropImageUri, 1, 1, OUTPUT_X, OUTPUT_Y, CODE_RESULT_REQUEST);
break;
}
}
获取选中图片的Uri
/**
* 解析后的Uri
*
* @param context
* @param uri
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public String getPath(Context context, Uri uri) {
boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
String pathHead = "file:///";
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return pathHead + Environment.getExternalStorageDirectory() + "/" + split[1];
}
} else if (isDownloadsDocument(uri)) {
String id = DocumentsContract.getDocumentId(uri);
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return pathHead + getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri)) {
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
String selection = "_id=?";
String[] selectionArgs = new String[]{split[1]};
return pathHead + getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return pathHead + getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return pathHead + uri.getPath();
}
return null;
}
private String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
private boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
private boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
private boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}