简体   繁体   中英

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);

        }

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());
    }
}

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM