This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Sending multiple images  RSS feed

 
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;
}
 
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?
 
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!