欢迎使用CSDN-markdown编辑器

来源:互联网 发布:内网锐捷认证软件 编辑:程序博客网 时间:2024/06/11 18:44

android开启系统相机或系统相册获取图片

打开系统相机代码有两种方式:

1,使用意图隐式调用

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent,code);

此方式拿到的图片不是原图.

2,也是使用意图,需要添加一个临时路径,用于存储拿到的图片.
    Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    File file=new File("tempPath");    Uri uri=Uri.fromFile(file);    intent.putExtra(MediaStroe.EXTRA_OUTPUT,uri);    startActivityForResult(intent,code);

这种方式拿到的图片是原图,需要压缩.否则有可能出现oom.

从写onActivityResult处理拿到的图片

     @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if(data==null){            if(requestCode==CAMERA_CODE) {//            Bundle extras = data.getExtras();//            Bitmap bitmap = (Bitmap) extras.get("data");//            headImag.setImageBitmap(bitmap);//            PhotoUtils.cropImageUri(this,data.getData(),600,600,0);                Bitmap bitmap = BitmapFactory.decodeFile(ConfigManager.IMG_PATH + "/head.png");                headImag.setImageBitmap(bitmap);            }            return;        }else if((requestCode==IMAGE_REQUEST_CODE)){//相册            Uri data1 = data.getData();            File fileWithUri = PhotoUtils.getFileWithUri(data1, this);            headImag.setImageBitmap(BitmapFactory.decodeFile(fileWithUri.getAbsolutePath()));//            PhotoUtils.cropImageUri(this,data.getData(),300,300,21);        }else if(requestCode==CAMERA_CODE) {            Bundle extras = data.getExtras();            Bitmap bitmap = (Bitmap) extras.get("data");            headImag.setImageBitmap(bitmap);//            PhotoUtils.cropImageUri(this,data.getData(),600,600,21);//            Bitmap bitmap = BitmapFactory.decodeFile(ConfigManager.IMG_PATH + "/head.png");//            headImag.setImageBitmap(bitmap);        }else if(requestCode==21){            Uri data1 = data.getData();            File fileWithUri = PhotoUtils.getFileWithUri(data1, this);            headImag.setImageBitmap(BitmapFactory.decodeFile(fileWithUri.getAbsolutePath()));        }        super.onActivityResult(requestCode, resultCode, data);    }
0 0
原创粉丝点击