Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet

How to catch the large of image size

hi i am getting image from backed and i am have problems with one image ( 1mb size or above) , how i can know the image size? then i just dont show that image, because i am getting a Outofmemory Error.

	throws IOException {
		byte[] responseData = new byte[1000];
		int length = 0;
		StringBuffer rawResponse = new StringBuffer();
		ConnectionThread connectionThred = new ConnectionThread(url);
		connectionThred.run();
		try {

			if (!connectionThred.isHttpConnectionOk()) {
				throw new IOException("HTTP response code: "
						+ connectionThred.getResponseCode());
			}
			inputStream = connectionThred.getInputStream();

			while (-1 != (length = inputStream.read(responseData))) {
				rawResponse.append(new String(responseData, 0, length));
			}

			final String result = rawResponse.toString();

			byte[] dataArray = result.getBytes();
			encodeImageBitmap = EncodedImage.createEncodedImage(dataArray, 0,
					dataArray.length);

		} catch (final Exception ex) {
			System.out.print("Exception (" + ex.getClass() + "): "
					+ ex.getMessage());
		} finally {
			try {
				inputStream.close();				
				connectionThred.close();				
			} catch (Exception e) {
			}
		}

		return GPATools.ResizeTransparentBitmap(encodeImageBitmap.getBitmap(),//
				width, height, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FIT);

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to catch the large of image size

The following is inefficient code that will use more memory than you need:

 

while (-1 != (length = inputStream.read(responseData))) {
rawResponse.append(new String(responseData, 0, length));
}
final String result = rawResponse.toString();
byte[] dataArray = result.getBytes();

 

replace this code with

 

byte [] dataArray = IOUtilities.streamToBytes(inputStream);

 

See if this helps. 

 

I suspect it won't, I suspect you will still fail at some point.  If you do, please tell us at what point you fail.  I suspect it will be on the conversion of the bytes to the Encoded Image.  A 1Mb image file, which I imagine you have compressed as a jpg or png, is quite some size.  What size are the pictures you are working with (width and height in pixels)?

Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet

Re: How to catch the large of image size

 at this point fail, and by the way the emulator just get close and no said anything about error now xD: 

 

byte[] dataArray = result.getBytes(); 


and after the chage u said above the fail is here :

 

return GPATools.ResizeTransparentBitmap(encodeImageBitmap.getBitmap(),//
width, height, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FIT);


 this is the size of the the problematic  image 1,17 MB (1.228.800 bytes) , and did not any compression of the image, is alredy png. finally in that las red method abouve i resize the image to width =50 and height

 

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to catch the large of image size

This seems pretty inefficient - you are sending down a image that is over 1M when compressed, and on screen it is going to be 50 x 50 - which as a Bitmap is probably about 10 Kb uncompressed.  In these circumstances, the best approach is to have the Server deliver an image that is closer to the size that you want.

 

ANyway, if I understand correctly, by using the more efficient code I gave you, you are getting past the problem with the processing of the input into a byte array, you are now able to convert it to an Encoded image:

itmap = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length);

But it is when you start trying to process the EncodedImage you run into memory problems.

 

There is only one way around this that I am aware of.  You will have to scale your bytes directly into a Bitmap.  You do that using

public static Bitmap createBitmapFromBytes(byte[] bytes,
                                           int offset,
                                           int length,
                                           int scale)

and you will have to specify a large scaling factor, I would suggest something like 5 or higher, but that will depend on the dimensions of the original input file. 

 

So your processing which currently returns an Encoded image, will return a Bitmap, but the Bitmap will be smaller. 

 

The scaling does not have to be completely accurate, you can use GPATools to do it perfectly, you are really only scaling it to get over the memory requirements of the full size image. 

 

Hope this works.

 

But the best solution is to get the Server to send a more reasonable size image.  Not only will that mean you can process it, but it will reduce the network transmission time considerably which will speed up the processing as far as the user is concerned.