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
salmanfarisu
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

image loading

Hi All

 

     How to load image from server one by one in to list field.

SalmanFaris
Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: image loading

for Get Image from Web URL Look at below Thread

 

 

http://supportforums.blackberry.com/t5/Java-Development/Get-image-from-url-Stream-closed/m-p/651411#...

 

And For Put Image in the ListField Look at this Below thread

 

http://supportforums.blackberry.com/t5/Java-Development/Custom-ListField-row-with-images-and-text/m-...

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

Please use plain text.
Contributor
ddgeek
Posts: 26
Registered: ‎11-30-2010
My Device: Not Specified

Re: image loading

Hey,

 

First you need to get data from server via web service.

 

Then you have to make one class which is having all field which ur web service have

 

Eg.

 

 

-<root>
   −<employee>
        <empno>1</empno>
        <empname>abc</empname>
        <empimage>http://abc.com/employee/images/NRA031B.jpg
        </empimage>
   </employee>
</root>

 

 

You need to fatch data from this webservice...

 

and set it to one of class

 

now getdata from class nd set it to bitmapfield

 

----------------------------------------------------------
feel free to press the kudos button on the left side to thank the user that helped you.
please mark posts as solved if you found a solution.

Please use plain text.
Developer
simon_hain
Posts: 15,842
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: image loading

@ddgeek: absolutely no need to include a webservice in this usecase.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
carlostheone
Posts: 151
Registered: ‎01-20-2010
My Device: Blackberry Bold
My Carrier: Telefonica

Re: image loading

Exactly , you dont need a webservice.

 

i found this code in other tread:

 

 

private void downloadimage(String urlImage) {
			
 HttpConnection connection = null;
 InputStream inputStream = null;
 EncodedImage _bmap;
 byte[] dataArray = null;
Bitmap image=null;

for(int i=0;i<numberImages;i++){
 try{
 connection = (HttpConnection) Connector.open(urlImage,Connector.READ_WRITE, true);
    	   inputStream = connection.openInputStream();
	   int length = 0;   
	    byte[] responseData = new byte[10000];
	     StringBuffer rawResponse = new StringBuffer();
		while (-1 != (length = inputStream.read(responseData))) {
		rawResponse.append(new String(responseData, 0, length));
					}
		int responseCode = connection.getResponseCode();
		if (responseCode != HttpConnection.HTTP_OK) {
						throw new Exception("HTTP response code: " + responseCode);
					}
final String result = rawResponse.toString();
					dataArray = result.getBytes();
				} 
				catch (final Exception ex) {
				}

				finally {
	
					try {
						inputStream.close();
						inputStream = null;
						connection.close();

					} catch (Exception e) {
					}
					
				}// fin de Finally
				_bmap = EncodedImage.createEncodedImage(dataArray, 0,	dataArray.length);
		image=_bmap.getBitmap();
			 }

 

ps: sorry for my english

 

Please use plain text.
Contributor
salmanfarisu
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

Re: image loading

 hi ddgeek

 

   This processes are working fine .i have more than  10 images in server, i need add image one by one in to listField. how is possible? if you have any sample code please send me it will be very help full.

 

 

Thanks in advance

 

SalmanFaris
Please use plain text.
Developer
simon_hain
Posts: 15,842
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: image loading

listFieldVector.addElement(x); listField.setSize(newSize); listField.invalidate();
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
ddgeek
Posts: 26
Registered: ‎11-30-2010
My Device: Not Specified

Re: image loading

I am going to write whole code of getting image file from server.

 

And set it to list field

 

 

-<root>
   −<employee>
        <empno>1</empno>
        <empname>abc</empname>
        <empimage>http://abc.com/employee/images/NRA031B.jpg
        </empimage>
   </employee>
</root>

 

Make one class:

 

 

 

public class Employee
{	
	public int id;
	public String empname;
	public String empimage;

	public int getEmpid()
	{
		return empid;
	}
	public void setEmpid(int empid)
	{
		this.empid = empid;
	}
	public String getEmpname()
	{
		return empname;
	}
	public void setEmpname(String empname)
	{
		this.empname = empname;
	}
	public String getEmpimage()
	{
		return empimage;
	}
	public void setEmpimage(String empimage)
	{
		this.empimage = empimage;
	}

}

 In your main class or screen you have to write these functions :

 

 

//get data from xml, set data to class set class to vector
	public Vector getData()
	{
		// define variables later used for parsing
		Document doc;
		StreamConnection conn;
		Employee e = null;
		Vector vEmp = new Vector();
		try
		{
			// providing the location of the XML file,
			// your address might be different
			String url = "http://abc.com/employee/getEmp";	
			if (DeviceInfo.isSimulator())
			{
				url = url + ";deviceSide=true";
			}
			conn = (StreamConnection) Connector.open(url);
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			docBuilder.isValidating();
			doc = docBuilder.parse(conn.openInputStream());
			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("category");
			for (int i = 0; i < list.getLength(); i++)
			{
				e = new Employee();
				Node item = list.item(i);
				if (item.getNodeType() != Node.TEXT_NODE)
				{
					NodeList itemChilds = item.getChildNodes();
					for (int j = 0; j < itemChilds.getLength(); j++)
					{
						Node detailNode = itemChilds.item(j);
						if (detailNode.getNodeType() != Node.TEXT_NODE)
						{
							if (detailNode.getNodeName().equalsIgnoreCase("empno"))
							{
								ctgr.setEmpid(Integer.parseInt(getNodeValue(detailNode)));
							}
							if (detailNode.getNodeName().equalsIgnoreCase("empname"))
							{
								ctgr.setEmpname(getNodeValue(detailNode));
							}
							if (detailNode.getNodeName().equalsIgnoreCase("empimage"))
							{
								ctgr.setEmpimage(getNodeValue(detailNode));
							}
						}

					}
				}
				vEmp.addElement(new Employee());
				vEmp.setElementAt(e, i);
				// v.addElement(e);
			}
		}// end try
		// will catch any exception thrown by the XML parser

		catch (Exception e)
		{
			System.out.println(e.toString());
		}
		return vEmp;
	}

 

//get data from vector, set data to listfield
	Vector getEmps = new Vector();
	getEmps = getData();
	BitmapField[] empImageField = null;

	private Vector listInfoVector = new Vector();

	private ListField empInfoListField;


	for (i = 0; i < getItemDetails.size(); i++)
	{
		Employee emp = (Employee) getData.elementAt(i);
		String imageURL = emp.getEmpimage();
		String empName = item.getEmpname();
		int empID = item.getEmpid();

		LabelField empIDLabel = new LabelField(iname, Field.FIELD_LEFT);
		LabelField empNameLabel = new LabelField(cname, FIELD_HCENTER);

		Bitmap itemImageBitmap = null;
		itemImageBitmap = getImageField(imageURL);
	}
	listInfoVector.addElement(new Info(empIDLabel, empNameLabel, itemImageBitmap));

	empInfoListField = new ListField(listInfoVector.size()) {
		//navigation click
	};
	empInfoListField.setCallback(this); // We manage the interaction!
	this.add(empInfoListField);

 

	public void drawListRow(ListField listField, Graphics g, int index, int y,
			int width)
	{
		Info infoToDraw = (Info) this.get(listField, index);

		g.drawText(infoToDraw.EmpNum, 90, y + 9);
		g.drawText(infoToDraw.EmpName, Display.getWidth() - 90, y + 9);

		// image of emp
		g.drawBitmap(10, y + 6, 50, 50, infoToDraw.EmpImage, 0, 0);
		g.drawLine(0, y, Display.getWidth(), y);
	}


 

	class Info
	{
		public LabelField EmpName;
		public LabelField EmpNum;
		public Bitmap EmpImage;

		public Info(LabelField eName, LabelField eNum, Bitmap image)
		{
			EmpName = eName;
			EmpNum = eNum;
			EmpImage= image;
		}

	}

 

	public BitmapField getImageField(String url) throws IOException
	{
		StreamConnection conn;
		if (DeviceInfo.isSimulator())
		{
			url = url + ";deviceSide=true";
		}
		conn = (StreamConnection) Connector.open(url);
		InputStream is = conn.openInputStream();
		BitmapField bf = null;
		try
		{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int ch;
			while ((ch = is.read()) != -1)
			{
				baos.write(ch);
			}
			byte imageData[] = baos.toByteArray();
			bf = new BitmapField(EncodedImage.createEncodedImage(imageData, 0, imageData.length).getBitmap());
		} finally
		{
			if (is != null)
			{
				is.close();
			}
		}
		return (bf == null ? null : bf);
	}

 

Full working example i provide you.

 

----------------------------------------------------------
feel free to press the kudos button on the left side to thank the user that helped you.
please mark posts as solved if you found a solution.

 

Please use plain text.
New Member
coolsmarty
Posts: 1
Registered: ‎12-09-2010
My Device: Not Specified

Re: image loading

Hey ddgeek,

 

Its perfect solution.

 

I appreciate  your work.

 

 

me: coolsmarty

Please use plain text.
Contributor
salmanfarisu
Posts: 17
Registered: ‎08-04-2010
My Device: Not Specified

Re: image loading

hi simon

 

 

 Thanks for the replay. actually  i am newbie in blackberry, can you please brief it  or send me the sample code if you have .

 if you have any  code sample link or something please feel free to provide me.

 

What i am doing in my code is

 

step1: Array of image URL

 

       String [] urls={"http:/ipaddress/dev/cap.png",
                                  "http://ipaddress/dev/Sweatshirt.png",
                                     "http://ipaddress/dev/tshirt.png",
                                      "http://ipaddress/dev/t-shirt.png",
                                        "http://ipaddress/dev/youth_jersey.png"};

Step 2:

         using "carlostheone's" code for loading the image display the bitmap, i got all the images from the server but it is taking so much time for displaying bitmaps.

 

What i need is

-------------------

First i want to display the listField which having Bitmap field

and want to display the bitmap one by one in the list field ...i guess this won't take too much of time ...

 

 

 

Thanks in advance

SalmanFaris
Please use plain text.