BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 11-19-2008, 03:00 AM   #1 (permalink)
New Member
 
Join Date: Sep 2008
Location: Málaga, Spain
Model: 8310
PIN: N/A
Carrier: dont know
Posts: 7
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default get Image from InputStream

Please Login to Remove!

The question is... how can I get an image from an InputStream correctly?

Using a Nokia N95 I can get that image with any of this methods:


PHP Code:
//1 Directly with createImage
Image image Image.createImage(in);

//2 Using readFully and a buffer

DataInputStream din = new DataInputStream(in);

imageData = new bytexxx91;imageSizexxx93;;
din.readFully(imageData);
image Image.createImage(imageData0imageData.length);

//3 Using chunks and ByteArrayOutputStream
DataInputStream din = new DataInputStream(in);

byte imageDataxxx91;xxx93; = new bytexxx91;256xxx93;;
while ( (
din.read(imageData)) != -) {
baos.write(imageData,0,i);
}
byte xxx91;xxx93; result baos.toByteArray();
Image image Image.createImage(result0result.length);

//3.1 The same as #3 changing the loop
while ( true ) {
int chunk din.read(imageData);
if (
chunk==-1) break;
baos.write(imageData,0,chunk);
}
baos.flush();
byte xxx91;xxx93; result baos.toByteArray();
Image image Image.createImage(result0result.length);

//4 One by one

DataInputStream din = new DataInputStream(in);

byte xxx91;xxx93; result = new bytexxx91;imageSizexxx93;;
for(
int i=0;i<imageSize;i++) {
resultxxx91;ixxx93; = (byte)din.read();
}
Image image Image.createImage(result0result.length);

//4.1 The same as #4 with some changes
DataInputStream din = new DataInputStream(in);

int i 0;
while (
imageSize ) {
baos.write(din.read());
i++;
}
baos.flush();
byte xxx91;xxx93; result baos.toByteArray();
Image image Image.createImage(result0result.length); 


All of them are tested and works with the Nokia phone, but with my BlackBerry 8310 I allways get an EOFException. I know that in any moment while reading the bytes of the image gets a value of -1 (EOF) but I can use these workarounds so the app doesn't throw any exception. Another issue that I know is the fact that, if I use an image with an uniform color (red), the code works because no -1 is returned . Any idea for the BlackBerry?
Offline  
Old 11-21-2008, 01:31 AM   #2 (permalink)
New Member
 
Join Date: Sep 2008
Location: Málaga, Spain
Model: 8310
PIN: N/A
Carrier: dont know
Posts: 7
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

After some hours of testing I realized that the problem is NOT to create the image but the control of the stream. I try to download the image from a FTP Bluetooth server and almost all the codes posted before work, but with images equals or lower than 4 KB. Example: image1.jpg (4015 bytes) OK, image2.jpg (4130 bytes) FAIL.

There are two ways thay I found reasonable:

1) Reading with an array of bytes. In the case of Nokia it reads in chunks of 1KB, but the BlackBerry tries to read everything and (with a file higher than 4KB) throws an EOFException.

PHP Code:
DataInputStream din = new DataInputStream(in);
byte xxx91;xxx93; result = new bytexxx91;imageSizexxx93;;
int bytes 0;
int offset 0;
while (
true) {
     
bytes din.read(resultoffsetresult.length offset);
     
offset += bytes;
     if (
bytes == -|| offset >= result.length) {
          break;
     }
}
Image image Image.createImage(result0result.length); 

2) Reading bytes one by one. You can think that, following the precedent reasoning, while reading the byte #4096 it will throw an EOFException but, throws the Exception in the first byte instead. I tried introducing a delay of 500ms every 1000 bytes read, but it still throws the Exception on the first byte. Is possible that the BlackBerry have an intermediate buffer (of 4KB) or something???

PHP Code:
DataInputStream din = new DataInputStream(in);
byte xxx91;xxx93; result = new bytexxx91;imageSizexxx93;;
for(
int i=0;i<imageSize;i++) {
    
resultxxx91;ixxx93; = (byte)din.read();
    if (
1000 == 0) {
         
//sleep(500);
    
}
}
Image image Image.createImage(result0result.length); 
Anyone?
Offline  
Old 11-21-2008, 07:16 AM   #3 (permalink)
New Member
 
Join Date: Sep 2008
Location: Málaga, Spain
Model: 8310
PIN: N/A
Carrier: dont know
Posts: 7
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Maybe something related to memory limitation?:
(I'm sorry, but I still can't post links, so I post without HTML format, HTTP and WWW)
"blackberryforums.com/general-7200-series-discussion/33485-blackberry-7290-task-truncation-memory-limitation.html#post226261"
PIM synch is executed over FTP?
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright © 2004-2014 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.