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
Contributor
Posts: 28
Registered: ‎04-08-2010
My Device: 9300
My Carrier: developer
Accepted Solution

Get image from url. Stream closed.

 

String url="http://.../image.jpg";								InputStream inputStream = (InputStream) Connector.openInputStream(url);
									 Bitmap image = null;						
BitmapField bf = null; try
{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); int ch; while ((ch = inputStream.read()) != -1)
{ baos.write(ch);
} byte[] imageData = baos.toByteArray();
image = Bitmap.createBitmapFromBytes(imageData, 0, imageData.length, 1); bf = new BitmapField(image); _fieldManagerMiddle.deleteAll(); _fieldManagerMiddle.add(bf); }
finally
{ if (inputStream != null) inputStream.close(); }
}

 

Hello, I want to add a image from url to a bitmap. The error is "Stream closed". The image is allocated in my server and it has read and write permissions.

Thanks

 

 

 

 

Developer
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Get image from url. Stream closed.

Use HttpConnection

            try
            {
                hc=(HttpConnection)Connector.open("http://localhost/Image.jpg");
                hc.setRequestMethod(HttpConnection.GET);
                int st=hc.getResponseCode();
                int len=(int)hc.getLength();
                System.out.println("Code==>"+hc.getResponseCode());
                System.out.println("Message==>"+hc.getResponseMessage());
                byte bts[]=new byte[len];
                dis=new DataInputStream(hc.openInputStream());
                dis.readFully(bts);                
                image=EncodedImage.createEncodedImage(bts,0,len);
                dis.close();
                hc.close();  

          }catch(Exception e){}

Now Your Image Downloaded and in image object.

And to show on the screen

 

            BitmapField bimage=new BitmapField();   
            bimage.setImage(image);
            add(bimage);

------------------------------------------------------------------------------------
Press Kudo to say thank to developer.
Also Press the Accept as solution Button when u got the Solution.

 

Developer
Posts: 237
Registered: ‎05-07-2010
My Device: 8520
My Carrier: Internet

Re: Get image from url. Stream closed.

just try the following.

		try
		{
			HttpConnection connection=(HttpConnection)Connector.open(url);
			int response=connection.getResponseCode();
			if(response==HttpConnection.HTTP_OK)
			{
				InputStream is=connection.openInputStream();
				int length=is.available();
				byte[] data=new byte[length];
				data=IOUtilities.streamToBytes(is);
				is.close();
				connection.close();
				image=Bitmap.createBitmapFromBytes(data,0,data.length,1);
			}
		}
		catch (Exception e) {
			System.out.println("Exception From Thread"+e);
		}

 

 

___________________________________________________
please press kudos(star button on left) if this post is helpful to you
"Accept as solution" if your query has been resolved.@hitendra
_________________________________________________