@cxm-2016
2016-12-15T09:36:24.000000Z
字数 1225
阅读 4827
Android
版本:1
作者:陈小默
声明:允许规范转载
/**
* 打开相机
*/
private val CAMERA = 1
/**
* 打开相册
*/
private val ALBUM = 2
/**
* 上传头像
*/
private val UPLOAD = 3
val intent = Intent(Intent.ACTION_PICK, null)
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
startActivityForResult(intent, ALBUM)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp)) //保存拍照后的相片到temp文件
startActivityForResult(intent, CAMERA)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
CAMERA -> {
if (resultCode == Activity.RESULT_OK) crop(Uri.fromFile(temp))
}
ALBUM -> {
if (resultCode == Activity.RESULT_OK) crop(data!!.data)
}
UPLOAD -> {
if (data != null && data.extras != null) {
val extras = data.extras
val head: Bitmap = extras.getParcelable("data")
...
}
}
else -> {
}
}
}
/**
* 打开系统裁剪页面
*/
private fun crop(uri: Uri) {
val intent = Intent("com.android.camera.action.CROP")
with(intent) {
setDataAndType(uri, "image/*")
putExtra("crop", "true")
putExtra("aspectX", 1) // aspectX aspectY 是宽高的比例
putExtra("aspectY", 1)
intent.putExtra("outputX", 150) // outputX outputY 是裁剪图片宽高
intent.putExtra("outputY", 150)
intent.putExtra("return-data", true)
}
view.startActivityForResult(intent, UPLOAD)
}