Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Administrator
MSohm
Posts: 14,281
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

How to get an Image form server?

Quote from the previous forums:


Posts: 84
From: India
Registered: Feb, 2007

How to get an Image form server?
Posted: Jun 20, 2007 9:59 AM
 
Hi,
I want to get am image form server using HttpConnection and display in my application,

I have used following code:

//GETTING INPUTSTREAM FORM CONNECTION CLASS.
_inputstream =connThread.inputStream;

byte[] data=null;
int x;
Bitmap bmap=null;

ByteArrayOutputStream os=new ByteArrayOutputStream();
while((x=_inputstream.read())!=-1)
{
os.write((byte)x);
}
//CONVERTING ByteArrayOutputStream TO BYTE ARRAY.
data=os.toByteArray();

//CONVERTING BYTE DATA TO BITMAP.
bmap = Bitmap.createBitmapFromBytes(data,0,data.length,1 );

//DIPLAYING BITMAP USING GRAPHICS OBJECT
graphics.drawBitmap(0,imageBorderUp, screenWidth,screenHeight, bmap, 0, 0);


But i am only getting an error "IllegalArgumentException". I have extends FullScreen in my application. As i am getting the image from server, I am unable to add the image to the project in the IDE. Pls help me to solve this problem.
Any help is appreciated.

Thanks.


 


peterstrange

Posts: 1,479
From: UK
Registered: Apr, 2005

Re: How to get an Image form server?
Posted: Jun 20, 2007 5:39 PM
 
Did you review the line in the doc which says
"Creates a bitmap from image data. See EncodedImage.getSupportedMIMETypes to determine what image formats are supported"?
What format is your image in?

Can I suggest that you convert it to png format on the server and try the createBitmapFromPNG method.

Alternatively if in jpg format, or other supported image format, code like the following will probably do the job.
EncodedImage jpegPic = EncodedImage.createEncodedImage(data, 0 , data.length);
Bitmap bm = jpegPic.getBitmap();

Next time, it would be helpful to know which statement threw the exception. I presume it was the createBitmaoFromBytes(..).

 

Biswas

Posts: 84
From: India
Registered: Feb, 2007

Re: How to get an Image form server?
Posted: Jun 21, 2007 10:46 AM
 
Hi Peterstrange,

Thanks a lot. I have solved the problem using StreamConnection. Could you tell me what could be the mistake in HttpConnection.

While i am trying to establish HttpConnection, Every time "conn.getResponseCode()" returning 504 error.

If possible pls tell me how can i use HttpConnection for the same.
Thanks.

 

Guest: vicky
Re: How to get an Image form server?
Posted: Jul 16, 2008 5:58 PM
 
hi Biswas,

how you solved the problem using streamconnection ?

thanx

 

Biswas

Posts: 84
From: India
Registered: Feb, 2007

Re: How to get an Image form server?
Posted: Jul 17, 2008 1:38 AM
 
Hi Vicky,

Check the bellow code to get am Image from server in byte format.

StreamConnection stream = null;
InputStream in = null;
synchronized(this)
{
try
{
stream = (StreamConnection)Connector.open(url);
in = stream.openInputStream();
}
catch(Exception e)
{
}
}
// Extract the data from the input stream.
byte[] data = new byte[256];
try
{
DataBuffer db = new DataBuffer();
int chunk = 0;
while ( -1 != (chunk = in.read(data)))
{
db.write(data, 0, chunk);
}
in.close();
// Here is ur image in byte format.
data = db.getArray();
}
catch(Exception e)
{
}

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
funkym
Posts: 39
Registered: ‎04-01-2008
My Device: Not Specified

Re: How to get an Image form server?

Thanks a lot :smileyhappy:
Please use plain text.
Administrator
MSohm
Posts: 14,281
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: How to get an Image form server?

Quote from the previous forum:


Guest: vicky
Re: How to get an Image form server?
Posted: Jul 18, 2008 11:42 AM
 
hi Biswas,

Thanx a lot that really helped me .Now i am getting image in form of bytes. But problem i am getting now to draw the image. Could you please help me on that?

thanx
vicky

 


What type of image are you downloading from the server?  If it is a Bitmap, you can use the Bitmap constructor that accepts a byte array to create the image.  If it is of another type you can use the EncodedImage.createEncoded image, which also accepts a byte array.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
guperez
Posts: 37
Registered: ‎07-17-2008
My Device: Not Specified

Re: How to get an Image form server?

Probably off-topic but it may help some people. If you just want to display the image to the user, you can create a dynamic link in the web server and pass just the web link to the BlackBerry applicaton. Then you ask the BlackBerry browser to download the image. The nice thing about this option is that it will automatically reduce the size of the image for you. Plus the BlackBerry browser has a lot of functionalities related to image that you may use.

 

Sorry if this is off-topic. Just wanted to present different perspectives to download image :smileyhappy:.

 

Take care. 

Please use plain text.
New Developer
mickymouse
Posts: 10
Registered: ‎09-24-2008
My Device: Not Specified

Re: How to get an Image form server?

Thanks a lot,

 It helped me a Lot

Please use plain text.