android替换图片代码,Android Studio更换图片:选择照片或拍照相关代码

弹出信息提示框代码AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("选择图片");

builder.setMessage("可以通过相册和照相来修改默认图片!");

builder.setPositiveButton("图库", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME);

gallery();

}

});

builder.setNegativeButton("拍照", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME);

camera();

}

});

builder.create();//创建

builder.show();//显示

从相册中选择照片代码:/**

* 从相册获取

*/

public void gallery() {

Intent intent=new Intent(Intent.ACTION_GET_CONTENT);//ACTION_OPEN_DOCUMENT

intent.addCategory(Intent.CATEGORY_OPENABLE);

intent.setType("image/jpeg");

if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.KITKAT){

startActivityForResult(intent, PHOTO_REQUEST_GALLERY);

}else{

startActivityForResult(intent, PHOTO_REQUEST_GALLERY2);

}

}

从相机中获取/**

* 从相机获取

*/

public void camera() {

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

// 判断存储卡是否可以用,可用进行存储

if (hasSdcard()) {

intent.putExtra(MediaStore.EXTRA_OUTPUT,

Uri.fromFile(new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME)));

}

startActivityForResult(intent, PHOTO_REQUEST_CAMERA);

}

//判断存储卡是否可用/**

* 判断SD卡是否可用

*

* @return

*/

private boolean hasSdcard() {

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

return true;

} else {

return false;

}

}

//通过返回值来设置照片/**

* 通过返回的值来设置图片

*/

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PHOTO_REQUEST_GALLERY2) {

if (data != null) {

// 得到图片的全路径

String path = GetPictureFromLocation.selectImage(getApplicationContext(),data);

crop(Uri.parse("file://"+path));//调用剪贴图片代码

}

} else if (requestCode == PHOTO_REQUEST_GALLERY) {

if (data != null) {

// 得到图片的全路径

String path = GetPictureFromLocation.getPath(getApplicationContext(), data.getData());

crop(Uri.parse("file://"+path));//调用剪贴图片代码

}

}else if (requestCode == PHOTO_REQUEST_CAMERA) {

crop(Uri.fromFile(tempFile)); //调用剪贴图片代码

}else if (requestCode == PHOTO_REQUEST_CUT) {

try {

Bitmap bitmap = BitmapFactory.decodeFile(tempFile.getPath());

Log.e("uri", Uri.fromFile(tempFile).toString());

head_image.setImageBitmap(bitmap);

} catch (Exception e) {

e.printStackTrace();

}

}

}

//剪贴图片源码:/**

* 剪切图片

*

* @function:

* @author:Jerry

* @date:2013-12-30

* @param uri

*/

private void crop(Uri uri) {

Log.e("URI", uri.getPath());

Log.e("URI", uri.toString());

// 裁剪图片意图

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(uri, "image/*");

intent.putExtra("crop", "true");

// 裁剪框的比例,1:1

intent.putExtra("aspectX", 1);

intent.putExtra("aspectY", 1);

// 裁剪后输出图片的尺寸大小

intent.putExtra("outputX", 150);

intent.putExtra("outputY", 150);

intent.putExtra("scale", true);//黑边

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));

// 图片格式

intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

intent.putExtra("noFaceDetection", true);// 取消人脸识别

intent.putExtra("return-data", true);// true:不返回uri,false:返回uri

startActivityForResult(intent, PHOTO_REQUEST_CUT);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值