[小记]获取相机或者相册中的图片,并且塞入ImageView中

 

获取相机或者相册中的图片,并且塞入ImageView中

跳转相机 一句代码的事情

startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 0);

跳转相册并且裁剪,要是不想裁剪 把裁剪的代码去掉即可

                    Intent intent = new Intent(
                    Intent.ACTION_GET_CONTENT);

                    intent.putExtra("return-data", true);
                    intent.setType("image/*");
                    // TODO 剪裁
                    intent.putExtra("crop", "circleCrop");
                    // TODO 裁剪比例
                    intent.putExtra("aspectX", 1);
                    intent.putExtra("aspectY", 1);
                    // TODO 裁剪大小
                    intent.putExtra("outputX", 240);
                    intent.putExtra("outputY", 240);
                    startActivityForResult(intent, 1);

我们通过startActivityForResult跳转并且选择图片之后 回到了onActivityForResult中

        if (requestCode == 0 || requestCode == 1) {
        if (data != null) {
            LogUtils.printLog("RedWolf", "从相机中返回的数据 data+" + data.toString());
            //从相册或相机中获取的照片
            LogUtils.printLog("RedWolf", "从相机中返回的数据 data+" + data.getExtras());

            Bitmap mBitmap = (Bitmap) data.getExtras().get("data");


            File localimg = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "srcs");
            if (!localimg.exists()) {
                localimg.mkdirs();
            }
            File iconfile = new File(localimg.getAbsolutePath() + "/usericon.jpg");
            if (iconfile.exists()) {
                iconfile.delete();
            }
            OutputStream out = null;
            try {
                out = new FileOutputStream(iconfile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            //2、写入本地
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            //3、塞入ImageView中
            mImageView.setImageBitmap(mBitmap);

获取相机或者相册中的图片,并且塞入ImageView中

posted @ 2016-03-19 15:21  RedWolfChao  阅读(138)  评论(0编辑  收藏  举报