Bitmap decoding problem

This is a problem when you pull pictures back from the camera to the Gallery View. After each photo call the Uri is placed in an array for processing by the Gallery View.  Problem arise with memory if you just process the bitmap from the jpeg in the phone. Here is a solution to reduce the size of the bitmap and load back to the gallery view.

 

public static Bitmap loadBitmap(String url) {
		Bitmap bitmap = null;
		InputStream in = null;
		BufferedOutputStream out = null;

		try {
			in = new BufferedInputStream(new URL(url).openStream(),
					IO_BUFFER_SIZE);

			final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
			out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
			copy(in, out);
			out.flush();

			final byte[] data = dataStream.toByteArray();
			BitmapFactory.Options opts = new BitmapFactory.Options();
			opts.inJustDecodeBounds = true;
			bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
			Log.e("optwidth", opts.outWidth + "");
			opts.inJustDecodeBounds = false;
			if (opts.outWidth > 500) {
				opts.inSampleSize = 4;
				bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
						opts);
			} else {
				bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
						opts);
			}

			// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

		} catch (IOException e) {
			Log.e(TAG, "Could not load Bitmap from: " + url);
		} finally {
			closeStream(in);
			closeStream(out);
		}

		return bitmap;
	}