javaandroidandroid-studiophotoimage-compression

How to take a photo in Android, using full image size(not thumbnail) and compress the image to bytes?


I don't know if I'm doing something wrong maybe I'm not using the image at all or I'm compressing the image in the wrong way because when I'm trying to send it to a server it responds me saying that the size is over 10 MB when my phone takes pictures jpg around 7-9 MB (In the Edit.java I have a comment that I before was using the thumbnail but needed to change it cause the thumbnail has a bad quality when I try to see it in a Desktop)

Here is my code:

AndroidManifest.xml

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.camera"></uses-feature>

<provider
            android:authorities="cam.com.example.fileprovider"
            android:name="android.support.v4.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_path"/>
        </provider>

file_path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">

    <external-path
        name="external"
        path="/"/>
    <external-files-path
        name="external_files"
        path="/"/>
    <cache-path
        name="cache"
        path="/"/>
    <external-cache-path
        name="external_cache"
        path="/"/>
    <files-path
        name="files"
        path="/"/>

</paths>

Edit.java

btn_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    REQUEST_IMAGE_CAPTURE = 1;
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    if (cameraIntent.resolveActivity(getPackageManager()) != null) {

                        File imageFile = null;
                        try{
                            imageFile=getImageFile();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                        if(imageFile!=null){
                            Uri imageUri = FileProvider.getUriForFile(Edit.this,"cam.com.example.fileprovider",imageFile);
                            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                            startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);

                        }

                    }
                } catch (Exception e) {
                        Toasty.warning(getApplicationContext(), IC, Toast.LENGTH_SHORT, true).show();

                }
            }

        });



public File getImageFile() throws IOException{
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageName = "jpg_"+timeStamp+"_";
        File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

        File imageFile = File.createTempFile(imageName,".jpg",storageDir);
        currentImagePath = imageFile.getAbsolutePath();
        return imageFile;
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            if (imagenString == null) {

                File imgFile = new File(currentImagePath);
                String path = imgFile.getAbsolutePath();
                Bitmap mybitmap = BitmapFactory.decodeFile(path);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                mybitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                byte[] byteArray = byteArrayOutputStream.toByteArray();
                imagenString = Base64.encodeToString(byteArray, Base64.DEFAULT);

/* **Before I was doing this, but the thumbnail has such a bad quality so needed to change it**

                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                byte[] byteArray = byteArrayOutputStream.toByteArray();
                imagenString = Base64.encodeToString(byteArray, Base64.DEFAULT);*/
            }
        }
    }

Solution

  • It is because you're converting your image to Base64-encoding string which makes the size of the data bigger.

    In the following code you're using a value of 100 for the compression, 100 is least compression and high quality.

    mybitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    

    You can try to compress more your image, but it reduces the quality a bit:

    mybitmap.compress(Bitmap.CompressFormat.PNG, 70, byteArrayOutputStream);