项目中,基本都有用户自定义头像或自定义背景的功能,实现方法一般都是调用系统相机–拍照,或者系统相册–选择照片,然后进行剪裁,最终设为头像或背景。
我直接贴上使用的代码吧!
这次偷懒了,没有做效果图,不过这是我试过的代码,可以,只不过不能选取原图,因为会导致OOM问题
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:
getPicFromCamera();
break;
case R.id.pic:
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) {
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 image = bundle.getParcelable("data");
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) {
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 {
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();
}
return null;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
.注意,因为权限问题,所以,调用本地相册时,容易出现不同机型会有不同的显示效果
如果你的出现了问题,不妨看一下我的解决方案
使用另一种隐示意图
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关于运行时权限的,仿微信选择的一个方案来实现这种需求!!
在分享一个工厂:PhotoFactory封装了调用相机拍照,从相册选取照片,压缩选取的照片,图片搜索等功能