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
shere
Posts: 18
Registered: ‎09-18-2013
My Device: developer
My Carrier: developer

Upload image to server

Hello Everyone,

 

I want to capture and upload an image to server without using webworks.Plz Help.

 

 

Thanz in Advance.

Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Upload image to server

That is a very open question, can you define what do you need help with?

 

I suggest you search the forum because questions related to the issues you will find doing this have been asked and answered in the past.

Please use plain text.
Contributor
Balcony
Posts: 17
Registered: ‎08-27-2013
My Device: Blackberry Torch 9860
My Carrier: *

Re: Upload image to server

Are you using blackberry java or webworks?

 

Please use plain text.
Contributor
shere
Posts: 18
Registered: ‎09-18-2013
My Device: developer
My Carrier: developer

Re: Upload image to server

blackberry java,if you have some sample coed then please post.
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Upload image to server

You can search for samples as well as I can, better actually since you know what specific parts of this question you are actaully needing help with. 

 

So search, find out as much as you can, try some things, and come back when you have a problem or a specific question. 

Please use plain text.
Contributor
Balcony
Posts: 17
Registered: ‎08-27-2013
My Device: Blackberry Torch 9860
My Carrier: *

Re: Upload image to server

Check Out this snippet. If it can help. It works well for me

 

 

	public static boolean uploadImage(String fileName, String url)
			throws Exception {

		FileConnection fc = (FileConnection) Connector.open(fileName);

		InputStream inputStream = fc.openInputStream();
		ByteArrayOutputStream bos = new ByteArrayOutputStream();

		int bufferSize = 1024;

		byte[] buffer = new byte[bufferSize];

		int length = 0;

		while ((length = inputStream.read(buffer)) != -1) {
			bos.write(buffer, 0, length);
		}

		byte[] imagedata = bos.toByteArray();

		inputStream.close();
		fc.close();

		HttpConnection conn = (HttpConnection) Connector.open(url,
				Connector.READ_WRITE);
		conn.setRequestMethod(HttpConnection.POST);
		conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,
				HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA + ";"
						+ [data you are sending with the image in json]+"");
		conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,
				String.valueOf(imagedata.length));
		conn.setRequestProperty(HttpProtocolConstants.HEADER_USER_AGENT,
				"Blackberry Mobile");
		conn.setRequestProperty("x-rim-transacode-content", "none");

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		OutputStream finalOut = conn.openOutputStream();
		out.write(imagedata);
		byte[] b = out.toByteArray();
		finalOut.write(b);
		out.flush();
		out.close();

		finalOut.flush();
		finalOut.close();

		conn.close();

		return true;
}
Please use plain text.
Contributor
shere
Posts: 18
Registered: ‎09-18-2013
My Device: developer
My Carrier: developer

Re: Upload image to server

Thanks for all replies i could manage to upload image,I figured it out myself
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Upload image to server

"I figured it out myself"

 

Excellent. 

 

Can you mark this as solved?  Instructions and other useful information in the Help Link you will find near the top of the screen.

Please use plain text.