项目中,基本都有用户自定义头像或自定义背景的功能,实现方法一般都是调用系统相机–拍照,或者系统相册–选择照片,然后进行剪裁,最终设为头像或背景。
我直接贴上使用的代码吧!
这次偷懒了,没有做效果图,不过这是我试过的代码,可以,只不过不能选取原图,因为会导致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 mPics;
private ImageView mIamge;
private static final int IMAGE_REQUEST_CODE = 0;
private static final int CAMERA_REQUEST_CODE = 1;
private static final int RESULT_REQUEST_CODE = 2;
private static final String SAVE_AVATORNAME = "head.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mCarema = (Button) findViewById(R.id.carema);
mCarema.setOnClickListener(this);
mPics = (Button) findViewById(R.id.pics);
mPics.setOnClickListener(this);
mIamge = (ImageView) findViewById(R.id.iamge);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.carema:
Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), SAVE_AVATORNAME)));
startActivityForResult(intentFromCapture, CAMERA_REQUEST_CODE);
break;
case R.id.pics:
Intent intentFromGallery = new Intent();
intentFromGallery.setType("image/*");
intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intentFromGallery, IMAGE_REQUEST_CODE);
break;
default:
break;
}
}
/**
* 回调结果处理
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED) {
switch (requestCode) {
case IMAGE_REQUEST_CODE:
startPhotoZoom(data.getData());
break;
case CAMERA_REQUEST_CODE:
startPhotoZoom(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), SAVE_AVATORNAME)));
break;
case RESULT_REQUEST_CODE:
if (data != null) {
getImageToView(data);
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 裁剪图片方法实现
*/
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
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, RESULT_REQUEST_CODE);
}
/**
* 保存裁剪之后的图片数据
*/
private void getImageToView(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
saveMyBitmap(photo);
Bundle bundle = data.getExtras();
if (bundle != null) {
Bitmap image = bundle.getParcelable("data");
mIamge.setImageBitmap(image);
String path = saveImage("head", image);
File file = new File(path);
}
}
}
/**
* 将头像保存在SDcard
*/
public void saveMyBitmap(Bitmap bitmap) {
File file = new File(Environment.getExternalStorageDirectory(), SAVE_AVATORNAME);
try {
file.createNewFile();
FileOutputStream fileOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
fileOut.flush();
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将图片保存到本地
*
* @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.PNG, 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
- 153
- 154
.注意,因为权限问题,所以,调用本地相册时,容易出现不同机型会有不同的显示效果
如果你的出现了问题,不妨看一下我的解决方案
使用另一种隐示意图
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