拍照上传、相册获取逻辑实现

最近项目中用到的,做个笔记备份一下,so,请往下看~

  • 相册及拍照上传部分
    /**
     * @Description 打开系统相册
     * @param null
     * @return void
     * @throws
     */
    private void album2Photo() {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, SYS_INTENT_REQUEST);

    }

    /**
     * @Description 调用相机拍照
     * @param null
     * @return void
     * @throws
     */
    private void camera2Photo() {
        String sdStatus = Environment.getExternalStorageState();
        /* 检测sd是否可用 */
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
            ToolUtils.showToast("SD卡不可用!");
            return;
        }
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        startActivityForResult(intent, CAMERA_INTENT_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SYS_INTENT_REQUEST && resultCode == RESULT_OK
                && data != null) {
            try {
                Uri uri = data.getData();
                Cursor cursor = getContentResolver().query(uri, null, null,
                        null, null);
                cursor.moveToFirst();

                String imageFilePath = cursor.getString(1);
                Log.i("yzh","File path is----->" + imageFilePath);

                FileInputStream fis = new FileInputStream(imageFilePath);
                bitmap = BitmapFactory.decodeStream(fis);

                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                Log.i("yzh","压缩前的宽高----> width: " + width + " height:"
                        + height);

                /* 压缩获取的图像 */
                account_headshot_img.setImageBitmap(compressBigBitmap(bitmap, false));

                fis.close();
                cursor.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else if (requestCode == CAMERA_INTENT_REQUEST
                && resultCode == RESULT_OK && data != null) {
            getPhoto4Camera(data);
        }
        super.onActivityResult(requestCode, resultCode, data);

    }

    /**
     * @Description 获取压缩后的bitmap,避免图片过大
     * @param Bitmap;boolean
     * @return Bitmap
     * @throws
     */
    private Bitmap compressBigBitmap(Bitmap bitmap, boolean isCamera) {
        Bitmap destBitmap = null;
        /* 图片宽度调整为100,大于这个比例的,按一定比例缩放到宽度为100 */
        if (bitmap.getWidth() > 80) {
            float scaleValue = (float) (80f / bitmap.getWidth());
            Log.i("yzh", "缩放比例:"+scaleValue);
            Matrix matrix = new Matrix();
            /* 针对系统拍照,旋转90° 避免横屏*/
            if (isCamera){
                //matrix.setRotate(90);
            }
            matrix.postScale(scaleValue, scaleValue);

            destBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), matrix, true);
            int widthTemp = destBitmap.getWidth();
            int heightTemp = destBitmap.getHeight();
            Log.i("yzh", "压缩后的宽高----> width: " + heightTemp
                    + " height:" + widthTemp);
        } else {
            return bitmap;
        }
        return destBitmap;

    }

    /**
     * @Description 拍照后获取照片
     * @param Intent
     * @return void
     * @throws
     */
    private void getPhoto4Camera(Intent data) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String name = formatter.format(System.currentTimeMillis()) + ".jpg";
        Log.i("yzh", "image name:" + name);
        Bundle bundle = data.getExtras();
        /* 获取相机返回的数据,并转换为Bitmap图片格式 */
        Bitmap bitmap = (Bitmap) bundle.get("data");
        FileOutputStream fileOut = null;

        String path = Environment.getExternalStorageDirectory().getPath();
        File file = new File(path + "/SCZW/");
        /** 检测文件夹是否存在,不存在则创建文件夹 **/
        if (!file.exists() && !file.isDirectory())
            file.mkdirs();
        String fileName = file.getPath() + "/" + name;
        Log.i("yzh", "camera file path:" + fileName);
        try {
            fileOut = new FileOutputStream(fileName);
            /* 把数据写入文件 */
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOut);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileOut == null)
                    return;
                fileOut.flush();
                fileOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        account_headshot_img.setImageBitmap(compressBigBitmap(bitmap, true));
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值