07-21-2010 05:21 AM
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! ![]()
Solved! Go to Solution.
07-21-2010 05:34 AM
U can try this.
int len=(int)hc.getLength();
byte bts[]=new byte[len];
It converts into bytes.
07-21-2010 05:35 AM
Sorry hc is the HttpConnecion Object.
07-21-2010 05:42 AM
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?
07-21-2010 06:11 AM
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.
07-21-2010 06:22 AM
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.
07-21-2010 06:44 AM - edited 07-21-2010 06:45 AM
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!
07-21-2010 07:01 AM
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.
07-21-2010 07:35 AM
or you can always use the standard API
byte[] buf = IOUtilities.streamToBytes(stream);
07-21-2010 08:30 AM
Perfect, exactly what I needed, it makes my question kind of dumb in fact.
Thanks a lot Plato! ![]()