0

I wrote a camera activity as Android's guide documents says. I saved the photo, then I just want to know the width and height of this photo. But I can't get it with BitmapFactory.decodeStream. Here's my code, anybody can help me?

private PictureCallback mPictureCallback = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
            //save the photo
        File pictureFile = new File("/sdcard/test/test.jpg");
        if(!pictureFile.exists()) {
            try {
                pictureFile.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }

            //get the photo's width and height
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            InputStream is = null;
            try {
                is = new FileInputStream("/sdcard/test/test.jpg");
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if(is != null) {
                Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
                int picWidth = bitmap.getWidth();
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }



        CameraActivity.this.setResult(RESULT_OK);
        CameraActivity.this.finish();
    }
};
abnormal
  • 13
  • 3

2 Answers2

0

Is this solution working for you ?

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

//Returns null, sizes are in the options variable
BitmapFactory.decodeFile("/sdcard/image.png", options);
int width = options.outWidth;
int height = options.outHeight;
//If you want, the MIME type will also be decoded (if possible)
String type = options.outMimeType;

Source : android: get image dimensions without opening it

Community
  • 1
  • 1
athom
  • 313
  • 1
  • 11
0

If you use inJustDecodeBounds set as true in BitmapOptions,
the decoder will return null (no bitmap), but the out fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

So if you want only the height width values and don't want the bitmap itself,
use this flag.
And for that also, use option

width = options.outWidth;
height = options.outHeight;

else use
options.inSampleSize = 1
if you want the non-scaled version of bitmap also.

If options.inSampleSize = x where x>1, the image returned will be 1/x of the orignal size, means scaled by x.

akkilis
  • 1,904
  • 14
  • 21