Check Android image size before load to memory
<p>I've created small app that works with images from gallery or camera. </p>

<p>It all works fine, but.
On device with small screen and small memory size (HTC Desire) I have some images downloaded in full size from other mobile phone, and they are much larger (8MP camera on that phone).</p>

<p>If I try to load that, for my small camera huge image, it will crash immediately. </p>

<p>So, how to implement some kind of check and to downsize that image, but still load it properly?</p>

<p>I do scale images down after they are loaded, but this is something that should be done before the crash appears. </p>

<p>Tnx.</p>

<pre><code>           InputStream in = null;
            try {
                in = getContentResolver().openInputStream(data.getData());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // get picture size.
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(in, null, options);
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // resize the picture for memory.
            int screenH = getResources().getDisplayMetrics().heightPixels; //800
            int screenW = getResources().getDisplayMetrics().widthPixels; //480
            int width = options.outWidth / screenW;
            int height = options.outHeight / screenH;

            Log.w("Screen Width", Integer.toString(width));
            Log.w("Screen Height", Integer.toString(height));

            int sampleSize = Math.max(width, height);
            options.inSampleSize = sampleSize;
            options.inJustDecodeBounds = false;
            try {
                in = getContentResolver().openInputStream(data.getData());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // convert to bitmap with declared size.
            Globals.INSTANCE.imageBmp = BitmapFactory.decodeStream(in, null, options);
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
</code></pre>
