Search code examples
androidandroid-camera-intentimage-size

save full size image android studio


I'm trying to save full-size image on database but the app keep saving image smallest than the original that I took with the camera. Here is the onActivityResult method:

case TAKE_AVATAR_CAMERA_REQUEST_DOCUMENTO:
        case TAKE_AVATAR_CAMERA_REQUEST_INFRACCION:

            if (resultCode == Activity.RESULT_CANCELED) {
                // Avatar camera mode was canceled.
            } else if (resultCode == Activity.RESULT_OK) {

                // Took a picture, use the downsized camera image provided by
                // default
                Bitmap cameraPic = (Bitmap) data.getExtras().get("data");
                if (cameraPic != null) {
                    try {


                        saveAvatar(cameraPic, requestCode);
                    } catch (Exception e) {
                        Log.e(DEBUG_TAG, "saveAvatar() with camera image failed.", e);
                    }
                }
            }
            break;


private void saveAvatar(Bitmap avatar, int pRequestCode) {
        String strAvatarFilename = "avatar.jpg";
        String sPreferenceFoto = GAME_PREFERENCES_AVATAR;
        String sNumeroActa = (new ActaConstatacionRules(this)).getNextNumeroActa();
        int _idButton = 0;
        switch (pRequestCode) {
        case TAKE_AVATAR_CAMERA_REQUEST_LICENCIA:
            strAvatarFilename = sNumeroActa + "_licencia.jpg";
            sPreferenceFoto = CURRENT_ACTA_FOTO_LICENCIA;
            _idButton = R.id.ImageButton_Licencia;
            break;
        case TAKE_AVATAR_CAMERA_REQUEST_DOCUMENTO:
            strAvatarFilename = sNumeroActa + "_documento.jpg";
            sPreferenceFoto = CURRENT_ACTA_FOTO_DOCUMENTO;
            _idButton = R.id.ImageButton_Documento;
            break;
        case TAKE_AVATAR_CAMERA_REQUEST_INFRACCION:
            strAvatarFilename = sNumeroActa + "_infraccion.jpg";
            sPreferenceFoto = CURRENT_ACTA_FOTO_INFRACCION;
            _idButton = R.id.ImageButton_Infraccion;
            break;
        default:
            Utilities.ShowToast(this, "Seleccion de Imagen Invalida");
            return;
        }
        File image = null;
        try {

            File sdCardDirectory = Environment.getExternalStorageDirectory();
            image = new File(sdCardDirectory, strAvatarFilename);
            FileOutputStream outStream;

            try {

                outStream = new FileOutputStream(image);
                avatar.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                /* 100 to keep full quality of the image */

                outStream.flush();
                outStream.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }



        } catch (Exception e) {
            Log.e(DEBUG_TAG, "Avatar compression and save failed.", e);
        }
 strAvatarFilename));
        if (image == null)
            return;
        Uri imageUriToSaveCameraImageTo = Uri.fromFile(image);

        Editor editor = mCurrenActaSettings.edit();
        editor.putString(sPreferenceFoto, imageUriToSaveCameraImageTo.getPath());
        editor.commit();

        // Update the settings screen
        ImageButton avatarButton = (ImageButton) findViewById(_idButton);
        String strAvatarUri = mCurrenActaSettings.getString(sPreferenceFoto, RESOURCE_SIN_FOTO);
        Uri imageUri = Uri.parse(strAvatarUri);
        avatarButton.setImageURI(null); // Workaround for refreshing an
                                        // ImageButton, which tries to cache the
                                        // previous image Uri. Passing null
                                        // effectively resets it.
        avatarButton.setImageURI(imageUri);
    }

I need to know how to save the (for example) 600 x 600 image size but without losing quality, now its saving 200 x 100 but I don't know why.


Solution

  • public File saveBitmapToFile(File file) {
        try {
    
            // BitmapFactory options to downsize the image
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inSampleSize = 6;
            // factor of downsizing the image
    
            FileInputStream inputStream = new FileInputStream(file);
            //Bitmap selectedBitmap = null;
            BitmapFactory.decodeStream(inputStream, null, options);
            inputStream.close();
            inputStream = null;
    
            int originalWidth = options.outWidth;
            int originalHeight = options.outHeight;
    
            if (originalWidth > 0) {
    
                int reqWidth = 600;
                int reqHeight = (reqWidth * originalHeight) / originalWidth;
                if (reqHeight >= 600)
                    reqHeight = 600;
                Log.d("new image ", "getDropboxIMGSize: " + reqHeight + "    " + 
     reqWidth);
                // decode full image pre-resized
                inputStream = new FileInputStream(file);
                options = new BitmapFactory.Options();
    
                // calc rought re-size (this is no exact resize)
                options.inSampleSize = Math.max(originalWidth / reqWidth, originalHeight 
     / reqHeight);
                // decode full image
                Bitmap roughBitmap = BitmapFactory.decodeStream(inputStream, null, 
     options);
    
                // calc exact destination size
                Matrix m = new Matrix();
                RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), 
     roughBitmap.getHeight());
                RectF outRect = new RectF(0, 0, reqWidth, reqHeight);
                m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
                float[] values = new float[9];
                m.getValues(values);
    
                // resize bitmap
                Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
                        (int) (roughBitmap.getWidth()*values[0]),
                (int) (roughBitmap.getHeight()*values[4]),true);
    
                // override resized bitmap image
                file.createNewFile();
                FileOutputStream out = new FileOutputStream(file);
                resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
            }
            return file;
    
        } catch (IOException e) {
            Log.e("Image", e.getMessage(), e);
            return null;
        }
      }