美文网首页
跳转到系统相机拍照

跳转到系统相机拍照

作者: 爱我O就直说 | 来源:发表于2020-04-24 10:21 被阅读0次

    2020-04-24

    //首先,别忘记 存储权限 的处理 android.permission.WRITE_SETTINGS
    checkPermission()

    
        String imgPath = "";
    
        //    跳转到相机
        public void toCamera(String fileName, String sdFolder) {
            
            if (!checkPermission()) return;
                    
            imgPath = SDCardUtils.getSDCardPathByEnvironment() + File.separator + sdFolder;
            File file = new File(imgPath);
            if (!file.exists()) file.mkdirs();
            imgPath = imgPath + File.separator + fileName;
    
            //传uri(图片保存地址),返回时data里没数据
            Uri outUri = UriUtils.file2Uri(new File(imgPath));
            startActivityForResult(IntentUtils.getCaptureIntent(outUri), 1);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            Log.i(TAG, "onActivityResult: " + imgPath);
            File file = new File(imgPath);
            Log.i(TAG, "onActivityResult: " + file.exists());
    
            if (resultCode == RESULT_OK) {
                switch (requestCode) {
                    //拍照功能
                    case 1:
                        // TODO: 做拍照后的处理
    
                        Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
    //                    Bitmap bitmap = ImageUtils.getBitmap(imgPath,target);
                        Log.i(TAG, "onActivityResult: " + bitmap.getWidth() + "~~" + bitmap.getHeight());
    
                        //质量压缩
                        byte[] bitmapByte = ImageUtils.compressByQuality(bitmap, 90);
                        Log.i(TAG, "onActivityResult: " + bitmapByte.length);
    
                        //宽高缩放压缩
                        bitmap = ImageUtils.compressByScale(bitmap, 600, 800);
                        byte[] bitmapByte2 = ImageUtils.compressByQuality(bitmap, 90); //再质量压缩
                        Log.i(TAG, "onActivityResult: " + bitmapByte2.length);
    
                        //写到文件
                        FileIOUtils.writeFileFromBytesByStream(file, bitmapByte2);
                        Log.i(TAG, "onActivityResult: " + file.length());
    
                        Glide.with(this).load(bitmapByte2).into(target);
    
                        break;
                }
            }
    
        }
    
        public static String getSDCardPathByEnvironment() {
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                return Environment.getExternalStorageDirectory().getAbsolutePath();
            }
            return "";
        }
    
        public static Intent getCaptureIntent(final Uri outUri) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            return intent;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:跳转到系统相机拍照

          本文链接:https://www.haomeiwen.com/subject/xsexwhtx.html