Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get image from URL using inputstream

 
Sam Venkata
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to get images from a url using a inputstream. The following is the sample code.
int length=connection.getContentLength();
//System.out.println("Content length : "+length);
inputStream=connection.getInputStream();
if (inputStream!=null)
{
blob=new byte[length];
inputStream.read(blob);
inputStream.close();
inputStream=null;
}

I am able to get the image but only part of the image is good and the other part is damaged(Some thing like a mosaic type).
Is there anything that I am missing.
Can anybody please help me.

Thanks,
Samanth.
 
Anupam Bhatt
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Samanth,

Could you please post the writing part of the code too ?
My guess is the during the write, you are not closing the outputstream or flushing it explicitly.

Having the code here would make it easy to help you !

Thanks,
Anupam
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never used content length. Do you think the numbers you get back are always reliable? I just read bytes until read returns -1.
 
Sam Venkata
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
My code looks something like this:

The class that I use to store it in the database is below:


It would be really helpful if anybody could help me with this.

Thanks,
Samanth.
[ June 25, 2007: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags to make your code more readable by preserving indentation. I have added them to your last post above. I would have added them to the first post, but the indentation there was very strange, and did not help readability for me.

Looking just at your code to read the array, I would say that the problem is you're not checking the return value of the read(). It's always possible for a single read() to be incomplete, and you need to put the method inside a loop if you want to ensure that it reads all of the bytes you want. E.g.

Or as Stan suggests, it's probably better to not trust the getContentLength(), and instead just read until the end:
 
Sam Venkata
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
Thanks a million that worked. I am really sorry about the indentation, I was not aware of it. I would make sure I use code tags next time.

Thanks,
Samanth.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic