今天手机升级Android7.0以后,发现,切换头像出错了。一脸懵逼,猜想肯定是升级Android7.0以后,又有了新的改动了,(虽然知道Android7.0会有改动,但是没想到,会在相机这么重要的功能上不兼容7.0以下)
一查发现,原来是因为7.0的打开相机的方式改变了,不再是Uri.fromFile(),那么7.0采用的是什么方式呢?
FileProvider没错,就是FileProvider:
使用FileProvider的方式打开相机
主要原理就是在应用间共享文件:
首先在AndroidManifest.xml的application中配置下面代码
android:name="android.support.v4.content.FileProvider"
android:authorities="opencamera.baochen.com.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
其次在res文件下面创建一个xml文件夹,在其中创建file_paths.xml文件。
file_paths.xml
name="image"
path="" />
下面就是Android打开相机的办法了。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.open_camera://打开相机
Intent openCameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //Android7.0以上
uri = FileProvider.getUriForFile(MainActivity.this, "opencamera.baochen.com.fileprovider", new File(Environment
.getExternalStorageDirectory(), "image.jpg"));//通过FileProvider创建一个content类型的Uri
openCameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
openCameraIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//将拍取的照片保存到指定URI
startActivityForResult(openCameraIntent, TAG_PHOTO_CAMERA);
} else {
uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));//7.0以下打开相机拍照的方法
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//保存照片
startActivityForResult(openCameraIntent, TAG_PHOTO_CAMERA);
}
break;
case R.id.open_album://打开相册
Intent openAlbumIntent = new Intent(
Intent.ACTION_GET_CONTENT);
openAlbumIntent.setType("image/*");
startActivityForResult(openAlbumIntent, TAG_PHOTO_ALBUM);
break;
}
}
裁剪:
/**
* 裁剪图片方法实现
*
* @param uri
*/
protected void startPhotoZoom(Uri uri) {
if (uri == null) {
Log.i("tag", "The uri is not exist.");
}
Intent intent = new Intent("com.android.camera.action.CROP");
//这里的uri是从亲们更具不同的方式获得的,所以才裁剪的时候,无需再进行Android7.0判断.
intent.setDataAndType(uri, "image/*");
// 设置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, CROP_SMALL_PICTURE);
}
文件夹根目录中保存的图片
全部代码:
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button openCamera;
Button openAlbum;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
openCamera = (Button) findViewById(R.id.open_camera);
openAlbum = (Button) findViewById(R.id.open_album);
openCamera.setOnClickListener(this);
openAlbum.setOnClickListener(this);
}
private static final int TAG_PHOTO_CAMERA = 0;//代表相机
private static final int TAG_PHOTO_ALBUM = 1;//代表相册
private static final int CROP_SMALL_PICTURE = 2;//裁剪照片
protected static Uri uri;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.open_camera://打开相机
Intent openCameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //Android7.0以上
uri = FileProvider.getUriForFile(MainActivity.this, "opencamera.baochen.com.fileprovider", new File(Environment
.getExternalStorageDirectory(), "image.jpg"));//通过FileProvider创建一个content类型的Uri
openCameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
openCameraIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//将拍取的照片保存到指定URI
startActivityForResult(openCameraIntent, TAG_PHOTO_CAMERA);
} else {
uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));//7.0以下打开相机拍照的方法
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//保存照片
startActivityForResult(openCameraIntent, TAG_PHOTO_CAMERA);
}
break;
case R.id.open_album://打开相册
Intent openAlbumIntent = new Intent(
Intent.ACTION_GET_CONTENT);
openAlbumIntent.setType("image/*");
startActivityForResult(openAlbumIntent, TAG_PHOTO_ALBUM);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) { // 如果返回码是可以用的
switch (requestCode) {
case TAG_PHOTO_CAMERA:
startPhotoZoom(uri); // 开始对图片进行裁剪处理(相机)
break;
case TAG_PHOTO_ALBUM:
if (data != null) {
startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理(相册)
}
break;
case CROP_SMALL_PICTURE:
if (data != null) {
setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上
}
break;
default:
break;
}
}
}
/**
* 裁剪图片方法实现
*
* @param uri
*/
protected void startPhotoZoom(Uri uri) {
if (uri == null) {
Log.i("tag", "The uri is not exist.");
}
Intent intent = new Intent("com.android.camera.action.CROP");
//这里的uri是从亲们更具不同的方式获得的,所以才裁剪的时候,无需再进行Android7.0判断.
intent.setDataAndType(uri, "image/*");
// 设置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, CROP_SMALL_PICTURE);
}
/**
* 保存裁剪之后的图片数据
*
* @param
* @param
*/
protected void setImageToView(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
photo = AvatarUtils.toRoundBitmap(photo, uri); // 这个时候的图片已经被处理成圆形的了
image.setImageBitmap(photo);
//上传到服务器等操作...
}
}
}
activity_main.xml
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
android:id="@+id/image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"/>
android:id="@+id/open_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="相机"/>
android:id="@+id/open_album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="相册"/>
AvatarUtils.java
public class AvatarUtils {
/**
* Save image to the SD card
*
* @param photoBitmap
* @param photoName
* @param path
*/
public static String savePhoto(Bitmap photoBitmap, String path,
String photoName) {
String localPath = null;
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File photoFile = new File(path, photoName + ".png");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100,
fileOutputStream)) { // 转换完成
localPath = photoFile.getPath();
fileOutputStream.flush();
}
}
} catch (FileNotFoundException e) {
photoFile.delete();
localPath = null;
e.printStackTrace();
} catch (IOException e) {
photoFile.delete();
localPath = null;
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
fileOutputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return localPath;
}
/**
* 转换图片成圆形
*
* @param bitmap
* 传入Bitmap对象
* @param tempUri
* @return
*/
public static Bitmap toRoundBitmap(Bitmap bitmap, Uri tempUri) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
left = 0;
top = 0;
right = width;
bottom = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
// final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);// 设置画笔无锯齿
canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas
paint.setColor(color);
// 以下有两种方法画圆,drawRounRect和drawCircle
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);//
// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
canvas.drawCircle(roundPx, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452
canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle
return output;
}
}
end-----------------------------------------------------------