无法压缩位图的Android Studio

时间:2018-06-28 20:45:59

标签: java android

在我的应用程序中,我有一个片段,可以打开设备的相机,并允许用户拍摄照片并将其存储在图库中的目录中。这是我用来创建目录并保存拍摄图像的方法:

    private void saveImage(Bitmap finalBitmap, String image_name) {

        final String appDirectoryName = "/Feel/";
        String root = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES).toString() + appDirectoryName;
        File myDir = new File(root);
        myDir.mkdirs();
        String fname = "Image-" + image_name + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists()) file.delete();
        Log.i("LOAD", root + fname);
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);外,其他所有功能都正常。当涉及到此行时,它会跳过该过程...也未创建目录...是什么引起了此问题?

这是我的CameraFragment,我在这里取回拍摄的照片:

 final Button btnShoot = viewCamera.findViewById(R.id.btnShoot);
    imgUltimaFoto.setVisibility(View.INVISIBLE);
    btnShoot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cmrView.addCameraListener(new CameraListener() {
                @Override
                public void onPictureTaken(final byte[] picture) {
                    super.onPictureTaken(picture);
                    CameraUtils.decodeBitmap(picture, new CameraUtils.BitmapCallback() {
                        @Override
                        public void onBitmapReady(Bitmap bitmap) {
                            imgUltimaFoto.setImageBitmap(bitmap);
                            imgUltimaFoto.setVisibility(View.VISIBLE);
                            saveImage(bitmap,"imagem1");
                        }
                    });
                }
            });
            cmrView.capturePicture();
        }
    });

0 个答案:

没有答案