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
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
Accepted Solution

How to show Image on Screen.

I am New in BB.Plz Help me.

I am facing the Problem is,I am fetching the Image from web.

But How can i show on the Screen.For Fetching I am writing the code like this,

If is any problem with this code then How can i, Plz Hepl me out.

 

import java.io.*;
import javax.microedition.io.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

class showImage extends MainScreen implements FieldChangeListener
{
    ButtonField down;
    Image img;
    ImageItem imit;
    public showImage()
    {    
        LabelField title=new LabelField("Image Download Example",LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH);
        setTitle(title);
        down=new ButtonField("Download");
        down.setChangeListener(this);
        add(down);
       
    }
    
    public Image startImage()
    {
        String url="http://media1.santabanta.com/full4/bikes/triumph/triumph-20d.jpg";
        HttpConnection hc=null;
        DataInputStream dis=null;
        try
        {
            hc=(HttpConnection)Connector.open(url);
            hc.setRequestMethod(HttpConnection.GET);
            int st=hc.getResponseCode();
            if(st==HttpConnection.HTTP_OK)
            {
                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);                
                img=Image.createImage(bts,0,len);
                dis.close();
                hc.close();                

          }

        }catch(Exception e){}
        return img;
    }
    public void fieldChanged(Field f,int c)
    {
        if(f==down)
        {
            Image image=startImage();
            add(image);
        }
    }
}

Please use plain text.
Developer
filiussoft
Posts: 106
Registered: ‎06-18-2009

Re: How to show Image on Screen.

okay - a few things:

 

1 - move your loading stuff onto its own thread. You are blocking the UI Thread.  Take a look at this thread: http://supportforums.blackberry.com/t5/Java-Development/Connecting-your-BlackBerry-http-and-socket-c...

 

2 - How are you adding the 'Image' to the MainScreen? add(...) takes an parameter of type Field.

 

3 - You should use a BitmapField

----
If this helped you, press the thumbs up button (on the far right - '+' thumb icon)
If this post solved the issue, click "Accept as Solution"
Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010

Re: How to show Image on Screen.

First I was Use BitMap Field But It was Showing the Error.

Please use plain text.
Developer
filiussoft
Posts: 106
Registered: ‎06-18-2009

Re: How to show Image on Screen.

'showing the error' - could you elaborate what kind of error / error message?

 

Can you compile your code? Is it a runtime error?

 

It kind of hard to help if you dont provide enough detail.

----
If this helped you, press the thumbs up button (on the far right - '+' thumb icon)
If this post solved the issue, click "Accept as Solution"
Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010

Re: How to show Image on Screen.

Can Anybody Give me the Helpful code.

 

I have fetched the Image From Web But Now How can i Display.

Plz.

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

Re: How to show Image on Screen.

Yup, It Showing the Error  not Runtime it's Build time.

 

BitmapField Not the campatible with Javax.microedition.lcdui.Image.

I was Using BitMapField fi=new BitMapField();

fi.setImage(image);

add(fi);

But this Showing the Above type Error.

Please use plain text.
Developer
filiussoft
Posts: 106
Registered: ‎06-18-2009

Re: How to show Image on Screen.

Take a look these classes:

 

 

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.image.Image;
import net.rim.device.api.ui.component.BitmapField;

 

 

This code should do what you need:

 

 

        EncodedImage image = EncodedImage.createEncodedImage(data, offset, length);
        BitmapField bitmapField = new BitmapField();
        bitmapField.setImage(image);
        add(bitmapField);

 

 

----
If this helped you, press the thumbs up button (on the far right - '+' thumb icon)
If this post solved the issue, click "Accept as Solution"
Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010

Re: How to show Image on Screen.

Thank u Sir, I will try All this Which u Provide me.

 

Thank u Once again.

Please use plain text.
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Carrier: Blackberry developer

Re: How to show Image on Screen.

yu need to download the image first . store the downloaded image in a vector then retrieve that image .

Please use plain text.