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
Highlighted
New Developer
Posts: 84
Registered: ‎01-28-2009
My Device: Not Specified
Accepted Solution

Fetching image from url

[ Edited ]

Hi to all and thanks for your support everytime,

 

I want to access a image from the url to my simulator. The page only contains an image which i want to fetch and load it to my simulator.

 

Before loading it to my simulator i have to resize it according to my wish and then keep it on my simulator.

Let me give you the key part that i just need the image from the whole page nothing else.

So if you can help me with it.

 

Thank You in advance,

Nimesh

Message Edited by nimesh on 03-19-2009 04:20 AM
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Fetching image from url

If you want the image of the size depending on you device, when you request to fetch the image from server let the server part use your user agents and create the image which fits for you on server and then read the image.

Use Search. "Accept Solution" If the problem is resolved.
New Developer
Posts: 84
Registered: ‎01-28-2009
My Device: Not Specified

Re: Fetching image from url

[ Edited ]

Thanks for your suggestion,

 

But let me inform you that I am working on BB simulator so i don't have any user agent.

I am on the development phase and I just need to fetch the image.

The image in the page has to be stored as Bitmap, BitmapField, or other field in simulator(recommended Bitmap).

 

Thank you,

Nimesh.

Message Edited by nimesh on 03-19-2009 04:21 AM
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Fetching image from url

You have a URL, Open the Connection, Open Inputstream on connection, read the data, Create the Bitmap using the data.

Use Search. "Accept Solution" If the problem is resolved.
New Developer
Posts: 84
Registered: ‎01-28-2009
My Device: Not Specified

Re: Fetching image from url

Thanks again,

 

 

I am sending you the code below so please inform me as to where i have to make corrections.

public RawHTMLBrowserSample() { MainScreen splashScreen = new MainScreen(); splashScreen.add( new RichTextField( "Loading..." )); pushScreen( splashScreen ); MyThread thread = new MyThread(); thread.start(); } private static class MyThread extends Thread { public void run() { try { // Open the connection and the input stream. Select your own URL. HttpConnection connection = (HttpConnection)Connector.open( url ); InputStream input = connection.openInputStream(); DataBuffer buffer = new DataBuffer(); byte[] temp = new byte[ 1024]; // Read in the web page to the DataBuffer. for( ;; ) { int bytesRead = input.read( temp ); if( bytesRead == -1 ) { break; } buffer.write( temp, 0, bytesRead ); } input.close(); connection.close(); // Create an output stream that will be used by the Base64 code. ByteArrayOutputStream output = new ByteArrayOutputStream(); Base64OutputStream boutput = new Base64OutputStream( output ); // Write out the special sequence which indicates to the browser // that it should treat this as HTML data in base64 format. output.write( "data:text/html;base64,".getBytes() ); boutput.write(buffer.getArray() ); boutput.flush(); boutput.close(); output.flush(); output.close(); // Invoke the browser with the encoded HTML content. BrowserSession bSession = Browser.getDefaultSession(); bSession.displayPage( output.toString() ); } catch( IOException e ) { System.out.println( "IOException: " + e ); } } }

 

 

Thanks,

Nimesh.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Fetching image from url

I am going to suggest it is not a good idea for us to provide you with corrected code for these reasons:

 

1) There seem to be to be a number of places in this code that could mean it doesn't work, correcting and explaining all of these will take a long time, certainly beyond the time I would give for free to someone - if you wanted me to correct code for a requirement like this I would ask you to pay for it. 

2) But most importantly I don't think it is good for you that you are given correct code you don't understand, I think it is better for you as a developer to work through the problem yourself so that you understand the solution. 

 

Instead I suggest that you divide this problem up into smaller pieces and resolve them one at a time.  You have three problems here:

 

a) Getting an image from an http page - to do this, review the httpdemo in the samples - it does exactly what you want

b) Convert the bytes into an image for display - I don't think there is a sample for this, but there are many posts on this forum related to this question - search and I think you will find out how to do this

c) Write the image to a file - here the fileexplorerdemo included with the samples will help you.

 

So, I recommend you get on with getting the httpdemo work for your URL and take it from there.

 

Just my 2p worth.

Developer
Posts: 57
Registered: ‎01-29-2009
My Device: Not Specified

Re: Fetching image from url

 

Hi Nimesh,

The  below link will surely solve your problem.

 

http://www.coderholic.com/category/blackberry/

 

Regards,

Vaikunth

New Developer
Posts: 19
Registered: ‎01-28-2010
My Device: 9700
My Carrier: no idea what this means

Re: Fetching image from url

I found this thread and read the solution, since I'm trying to do for Blackberry what I would do with one line in a j2me Midlet:

 

                                          Image image = this.getImageFromUrl(myurl);

 

I have a url and would like to display the Image when clicking on a button.

 

Do I really have to write all that code and the extra class for the connection?

Or I only missed something and there is an equivalent method to retrieve an image?

How can it be such a complicated thing?

 

(I just need to know that there is no other way..)

 

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Fetching image from url

You or someone else has to write the code, there is no native supplied method to do this for you.

 

Also I don't know J2ME very well, but I'm betting that

 

getImageFromUrl()

 

is not a standard J2ME function.  I think you will find someone has written this for you, for example:

http://www.java-tips.org/java-me-tips/midp/how-to-download-an-image-from-a-web-server.html

 

Note that this is not production quality code.

 

Re the link supplied, please review the comments - I do not think it is production quality code either.

New Contributor
Posts: 3
Registered: ‎08-27-2012
My Device: ????
My Carrier: ????

Re: Fetching image from url

[ Edited ]

The url from the "solution" gives a 404. Smiley Sad