Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending multiple images

 
Jari Kemppinen
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm attempting to send multiple images stored on the memory card on the phone to a server. I am sending one image at a time as a byte array. Everything works fine through an emulator but when I try it on the phone I get an array index out of bounds error after the first image has been sent through. I have narrowed it down to the following method that I believe is producing the problem. Is there a better way to retrieve an image as a byte array from the memory card? The phone is a Nokia 6630. Thanks in advance.

public byte[] getImage(String imgName)
{

int CHUNK_SIZE = 512;
String filePath = "file:///e:/Images/" + imgName;
byte[] imageData = new byte[0];

try
{
FileConnection fileConn = (FileConnection)Connector.open(filePath, Connector.READ);

// load the image data in memory
// Read data in CHUNK_SIZE chunks
InputStream fis = fileConn.openInputStream();
long overallSize = fileConn.fileSize();
int length = 0;

while (length < overallSize)
{
byte[] data = new byte[CHUNK_SIZE];
int readAmount = fis.read(data, 0, CHUNK_SIZE);
byte[] newImageData = new byte[imageData.length + CHUNK_SIZE];
System.arraycopy(imageData, 0, newImageData, 0, length);
System.arraycopy(data, 0, newImageData, length, readAmount);
imageData = newImageData;
length += readAmount;
}

fis.close();
fileConn.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
return imageData;
}
 
Fred Grott
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure can you post the full error message you get?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Right offhand, I don't see where the imageData array gets redefined to actually be able to hold data - is there something missing from your code?
Bill
 
Jari Kemppinen
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error I receive from the phone is
"-1 java.lang.IndexOutOfBoundsException".
Unfortunately there is no debugging tools for the phone to give me a more detailed error message.

I tried redefining the array with no success
imageData = new byte[(int)overallSize];

I also classed out the getImage method and called a new instance for every image, with no luck. Whats frustrating is that it works without a glitch with every attempt on an emulator but comes up with the above error on the phone everytime. I will just keep trying everything I can. Thanks for your opinions on this. Any other suggestions are welcomed.
 
Jari Kemppinen
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found the problem. It was within the method that was actually sending the image. At least this made me understand the fileconnection api a little better
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AHA - the -1 as an index error should have been a clue because the file read may return a -1 that you should check for.
This just shows that you should always include the EXACT exception when asking for suggestions.
Bill
 
Jari Kemppinen
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice Bill. I'll do exactly that in future posts.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!