1.清单文件配置
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
2.逻辑代码实现
private Button mCarema;
private Button mPic;
private ImageView mImage;
private String icon = "上传地址";
//相册请求码
private static final int ALBUM_REQUEST_CODE = 1;
//相机请求码
private static final int CAMERA_REQUEST_CODE = 2;
//剪裁请求码
private static final int CROP_REQUEST_CODE = 3;
//调用照相机返回图片文件
private File tempFile;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.carema:
// TODO 18/03/22
getPicFromCamera();//调用相机
break;
case R.id.pic:
// TODO 18/03/22
getPicFromAlbm();//调用相册
break;
default:
break;
}
}
/**
* 回调接口
*
* @param requestCode
* @param resultCode
* @param intent
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
// 调用相机后返回
case CAMERA_REQUEST_CODE:
if (resultCode == RESULT_OK) {
//用相机返回的照片去调用剪裁也需要对Uri进行处理
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(Main2Activity.this, "com.hansion.chosehead", tempFile);
cropPhoto(contentUri);//裁剪图片
} else {
cropPhoto(Uri.fromFile(tempFile));//裁剪图片
}
}
break;
//调用相册后返回
case ALBUM_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Uri uri = intent.getData();
cropPhoto(uri);//裁剪图片
}
break;
//调用剪裁后返回
case CROP_REQUEST_CODE:
Bundle bundle = intent.getExtras();
if (bundle != null) {
//在这里获得了剪裁后的Bitmap对象,可以用于上传
Bitmap image = bundle.getParcelable("data");
//设置到ImageView上
mImage.setImageBitmap(image);
//也可以进行一些保存、压缩等操作后上传
String path = saveImage("userHeader", image);
File file = new File(path);
/*
*上传文件的额操作
*/
}
break;
}
}
/**
* 从相机获取图片
*/
private void getPicFromCamera() {
//用于保存调用相机拍照后所生成的文件
tempFile = new File(Environment.getExternalStorageDirectory().getPath(), System.currentTimeMillis() + ".jpg");
//跳转到调用系统相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//判断版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //如果在Android7.0以上,使用FileProvider获取Uri
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(Main2Activity.this, "com.hansion.chosehead", tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
} else { //否则使用Uri.fromFile(file)方法获取Uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
}
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
/**
* 从相册获取图片
*/
private void getPicFromAlbm() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, ALBUM_REQUEST_CODE);
}
/**
* 裁剪图片
*/
private void cropPhoto(Uri 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");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_REQUEST_CODE);
}
/**
* 保存图片到本地
*
* @param name
* @param bmp
* @return
*/
public String saveImage(String name, Bitmap bmp) {
File appDir = new File(Environment.getExternalStorageDirectory().getPath());
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = name + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
ret
.注意,因为权限问题,所以,调用本地相册时,容易出现不同机型会有不同的显示效果
如果你的出现了问题,不妨看一下我的解决方案
使用另一种隐示意图
case R.id.pics:
// TODO 18/03/17
//选择本地图片
Intent intentFromGallery = new Intent();
intentFromGallery.setType("image/*"); // 设置文件类型
intentFromGallery.setAction(**Intent.ACTION_GET_CONTENT**);
startActivityForResult(intentFromGallery, IMAGE_REQUEST_CODE);
break;
将Intent.ACTION_GET_CONTENT改为Intent.ACTION_PICK
好了,简单的已经应用了,后续,我会加上适配Android6.0关于运行时权限的,仿微信选择的一个方案来实现这种需求!!