zoukankan      html  css  js  c++  java
  • [小记]获取相机或者相册中的图片,并且塞入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中

  • 相关阅读:
    开挂的列表与矜持的元组
    烦人的字符串
    好用的for循环与range
    浅谈编码
    流程控制与循环
    基础运算符
    python初识
    python的小介绍
    来自极客标签10款最新设计素材-系列九
    chmod----改变一个或多个文件的存取模式(mode)
  • 原文地址:https://www.cnblogs.com/redwolf/p/5295291.html
Copyright © 2011-2022 走看看