sitanshu
sitanshu

Reputation: 65

compress image while taking an image in android

Hello I am developing a CAMERA APPLICATION. After taking an image the image will be save in sdcard. I want while saving in sdcard the size of the image should be 400kb to 500kb but now its taking more than 1mb. How to compress and save in sdcard after capturing the image.

My code is

 public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            imageName="MyCameraApp" + String.valueOf(System.currentTimeMillis()) + ".jpg";
            File file = new File(Environment.getExternalStorageDirectory()+"/pictures", imageName);
            fileUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

        }

Upvotes: 0

Views: 2252

Answers (2)

Shahinoor Shahin
Shahinoor Shahin

Reputation: 685

I faced same problem and now I fixed it. I think it will be helpful for you. If you change the directory of your sdcard I think this code will work fine.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_optimization);

    String dirname = Environment.getExternalStorageDirectory() + "/shahin/";

    File sddir = new File(dirname);
    if (!sddir.mkdirs()) {
        if (sddir.exists()) {
        } else {
            Toast.makeText(ImageOptimizationActivity.this, "Folder error", Toast.LENGTH_SHORT)
                    .show();
            return;
        }
    }

    try {
        Bitmap bitmap = null;
        File file = new File(Environment.getExternalStorageDirectory()
                + "/DCIM/101SHARP/rubon.jpg");
        try {
            bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        FileOutputStream fos = new FileOutputStream(dirname + "output.jpg");
        bitmap.compress(CompressFormat.JPEG, 75, fos);

        fos.flush();
        fos.close();
    } catch (Exception e) {
        Log.e("MyLog", e.toString());
    }
}

}

Upvotes: 2

SubbaReddy PolamReddy
SubbaReddy PolamReddy

Reputation: 2113

try this:

String root = Environment.getExternalStorageDirectory()
                    .toString();
            File myDir = new File(root + "/_images");
            myDir.mkdirs();
            Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);
            String fname = "Image-" + n + ".jpg";
            file = new File(myDir, fname);
            Log.i(TAG, "" + file);
            if (file.exists())
                file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

where bm is bitmap image

Upvotes: 0

Related Questions