调用相机
Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent,TAKE_PHOTO);
调用相册两种
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
// 设置文件类型
intent.setType("image/*");
activity.startActivityForResult(intent, requestCode);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
// 设置文件类型
intent.setType("image/*");
activity.startActivityForResult(intent, requestCode);
详细代码请看(解析封装的uri获取真实图片路径那一块还没看懂,有空看看)
https://github.com/18668197127/PhotoAndGallery
两者的区别
如果你有一些特定的集合(由URI标识)想让用户选择,使用ACTION_PICK。
如果让用户基于MIME Type选择数据,使用ACTION_GET_CONTENT。
在平局的情况下,建议使用ACTION_GET_CONTENT。
调用系统的图片剪裁
//剪裁图片
protected void startPhotoZoom(Uri uri) {
if (uri == null) {
Log.i("tag", "The uri is not exist.");
}
// tempUri = uri;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setDataAndType(uri, "image/*");
// 设置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
//true直接返回一个Bitmap
intent.putExtra("return-data", false);
//设置格式
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//是否保留图片比例
intent.putExtra("scale", true);
File out = new File(getExternalStorageDirectory(),"crop1.jpg");
if (out.exists()){
out.delete();
try {
out.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if (Build.VERSION.SDK_INT>=24){
cropFinishUri=FileProvider.getUriForFile(MainActivity.this,"fileProvider",out);
//这段代码不知道什么作用,先放着以后研究
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Log.i(TAG, "ResolveInfo的Size: "+resInfoList.size());
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, cropFinishUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}else {
cropFinishUri=Uri.fromFile(out);
}
Log.i(TAG, "startPhotoZoom: "+cropFinishUri);
//设置输出到的uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropFinishUri);
startActivityForResult(intent, CROP_SMALL_PICTURE);
}
(参考别人的代码,还未自己研究过)
cropFinishUri=FileProvider.getUriForFile(MainActivity.this,"fileProvider",out);使用FileProvider封装过的Uri保存文件时报错,使用cropFinishUri=Uri.fromFile(out);方法可以,不知道为什么
如果使用FileProvider封装的Uri加上以下代码也可以实现
(FileProvider封装的Uri需要用grantUriPermission手动赋予临时权限)
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, cropFinishUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
获取拍照的图片发生旋转的情况
解决 拍照之后手动调整回来:
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap bitmap1=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
获取旋转角度(有待研究)根据获取的旋转度数,再手动旋转回来
(小米8手机自动旋转90度,华为P10手机正常)
String filePath=new File(getExternalStorageDirectory(),"take_photo1.jpg").getPath();
ExifInterface exifInterface = new ExifInterface(filePath);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.i(TAG, "onActivityResult: orientation "+orientation);
这里我的小米手机 orientation 值为6,代表向左旋转90°,华为手机orientation 值为0,代表不明确,实际为正常