1

I have one application in which mobile capture photo then it upload to the server. I want to check the size of photo first then reduce photo size to some particular size depending on actual photo size. Actually on server it not allow photo which have greater than some particular size (say 200 kb).

Or can I restrict the camera to capture photo only in particular size even some one change the setting of camera.

lot of question ask here which retrieve width and height of the photo. But I want to customize size in byte.

To open the camera I an using cameraIntent

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
Vinit ...
  • 1,409
  • 10
  • 37
  • 66
  • 2
    when you change the width and height of the photo, then obviously the size in bytes will also change. Did you give it a try?? – Suleman Khan Oct 29 '12 at 06:09
  • ok I will try this but I want to remain the height and width of the photo... – Vinit ... Oct 29 '12 at 06:12
  • i don't think it is possible to change the size with height and width unchanged. Might be possible but the quality of the image will also reduce respectively. – Suleman Khan Oct 29 '12 at 06:27

3 Answers3

2

This is the only possible solution AFAIK, to reduce the size..

public void onActivityResult(int requestcode, int resultcode, Intent data) {
        if (resultcode == RESULT_OK) {
            if (requestcode == SELECT_PICTURE) {
                Uri uri = data.getData();
                String imagePath = getPath(uri);
                int SCALE = 2;
                try{

                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = SCALE;
                Bitmap bitmap= BitmapFactory.decodeFile(imagePath, o2);
                OutputStream os = new FileOutputStream(imagePath);
                bitmap.compress(Bitmap.CompressFormat.PNG, 85, os);
                os.flush();
                os.close();
                File file = new File(imagePath);
                Log.i("Bitmap", "Height Width "+bitmap.getHeight()+" "+bitmap.getWidth());
                Log.i("File","Size in bytes "+file.length());
                while(file.length()>100*1024)
                {
                    SCALE +=2;
                    o2.inSampleSize = SCALE;
                    bitmap= BitmapFactory.decodeFile(imagePath, o2);
                    os = new FileOutputStream(imagePath);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 85, os);
                    os.flush();
                    os.close();
                    file = new File(imagePath);
                    Log.i("Bitmap", "Height Width "+bitmap.getHeight()+" "+bitmap.getWidth());
                    Log.i("File","Size in bytes "+file.length());
                }
                bitmap = BitmapFactory.decodeFile(imagePath, o2);

                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                txtimagepath.setText(imagePath);

            }
        }
    }



protected String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int columnIndex = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(columnIndex);

    }
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • of course , It's a String. I have updated the answer, [FYI as you already got bitmap from camera/gallery I thought you know it..and second but more important one "?" would do..no need of 4.] – MKJParekh Oct 29 '12 at 07:26
  • what is this getPath(uri) method, what is code in this method?? – Vinit ... Oct 29 '12 at 10:04
  • thanx 4 above code but some problem is there. First of all managedQuery method is deprecated and because of this line it throwing NullPointerException – Vinit ... Oct 29 '12 at 10:42
  • Its deprecated and that's why its throwing NPR? Anyways.. Working fine just tested again. – MKJParekh Oct 29 '12 at 13:18
1

You can't dynamically specify the size of the resulting file and do a scaling. You have to re-size the height and width, which in turn would reduce the file size.

May be, a recurring approach, that can finally keep the file size within your limits would help. I can't think of any other solution. At-least, the Android SDK doesn't provide such an API.

If you want to retain the width/height, then you could try compressing techniques, and that would result in loss in quality, and eventually, file size. But again, there is no direct API to achieve this.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
0
Bitmap bt=Bitmap.createScaledBitmap(YourOrignalBitmap,320, 300, false);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bt.compress(Bitmap.CompressFormat.PNG,100, bos);

here you can fix the size and maintain image Quality..

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75