If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
Contributor
Benjen
Posts: 33
Registered: ‎05-12-2010
My Device: none
My Carrier: none
Accepted Solution

Convert InputStream to Byte array

Hey everyone,

 

I really need some help with this issue I cannot get rid of! I am trying to make an app that loads an image saved on the BB device and that sends it to a server, the problem is that we can only send a byte[].

 

So i am trying to implement the following method:

- Load the image via the FileConnection class

- Open an InputStream of the said FileConnection

- Convert that InputStream to a byte array

 

The code should look like this I guess:

 

FileConnection file = (FileConnection)Connector.open("Original_SealV.jpg");
InputStream is=file.openInputStream();

byte[] img=null;
int temp=is.read();
while(temp>0){

// Convert to byte here?

}

 

It is the last step which I am lacking, i have tried several methods but could not get it to work, thus i am imploring for a little bit of the vast collective wisdom of these forums...

 

Could anyone help me? I won't forget the kudos! :smileyhappy:

 

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Convert InputStream to Byte array

U can try this.

 

int len=(int)hc.getLength();

byte bts[]=new byte[len];

 

It converts into bytes.

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Convert InputStream to Byte array

Sorry hc is the HttpConnecion Object.

Please use plain text.
Contributor
Benjen
Posts: 33
Registered: ‎05-12-2010
My Device: none
My Carrier: none

Re: Convert InputStream to Byte array

Thanks for the quick reply, but the problem is actually the other way around: I am trying to load an image on the Blackberry and then upload it, but prior to uploading I need to convert this image to a byte[].

 

If I misunderstood your answer, could you explain a bit more?



 

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Convert InputStream to Byte array

U r reading Image from Real Device Not from web.

 

So same Procedure r there to Convert Image in the Bytes Array.

Like U r Using code for reading Image form Web.

Same Procedure should be USed from real device.

This is not the Other things.

 

Please use plain text.
Contributor
Benjen
Posts: 33
Registered: ‎05-12-2010
My Device: none
My Carrier: none

Re: Convert InputStream to Byte array

Okay, could you give me the full code or just state the algorythm then? I cannot do a .getLength() on my FileConnection, and I don't really understand your solution either, in fact.

Please use plain text.
Contributor
Benjen
Posts: 33
Registered: ‎05-12-2010
My Device: none
My Carrier: none

Re: Convert InputStream to Byte array

[ Edited ]

I have tried doing something like this, is that what you meant?

 

InputStream is=file.openInputStream();
int len=(int)file.fileSize();
byte[]img=null;
is.read(img,0,len);

 

Any other participation would be welcomed!

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Convert InputStream to Byte array

My Mean is this

 

                hc=(HttpConnection)Connector.open(url);
                hc.setRequestMethod(HttpConnection.GET);
                int len=(int)hc.getLength();
                byte bts[]=new byte[len];

 

Now the Image in the bts in Byts Format.

Please use plain text.
Regular Contributor
Plato
Posts: 71
Registered: ‎07-11-2010
My Device: Bold 9000
My Carrier: none

Re: Convert InputStream to Byte array

or you can always use the standard API

 

byte[] buf = IOUtilities.streamToBytes(stream);


-----
A good decision is based on knowledge and not on numbers.
Please use plain text.
Contributor
Benjen
Posts: 33
Registered: ‎05-12-2010
My Device: none
My Carrier: none

Re: Convert InputStream to Byte array

Perfect, exactly what I needed, it makes my question kind of dumb in fact.

 

Thanks a lot Plato! :smileywink:

Please use plain text.