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
Regular Contributor
gillaraz
Posts: 69
Registered: ‎10-19-2010
My Device: BB8520

Display html image on Custom BrowserField

I'm using this custom browserfield

 

http://supportforums.blackberry.com/t5/Java-Development/Display-HTML-in-a-Screen-Field/m-p/337163

 

and facing unable to display image on the field.

 

It shows only the name and frame as picture below

 

 

I tried to search the forum and found this thread

http://supportforums.blackberry.com/t5/Java-Development/Problem-of-custom-browserfield-to-show-html-...

 

They said to set requestproperty httpconnection of custombrowserfield

 

I added to the httpconnection of custombrowserfield

 

connection.setRequestProperty("Accept","text/html, image/jpg, image/jpeg, image/");

 

but it shows stackoverflow error.

 

Could you help me to solve this issue ? I find the solution more than 6 hours and not get it.

Please use plain text.
Regular Contributor
gillaraz
Posts: 69
Registered: ‎10-19-2010
My Device: BB8520

Re: Display html image on Custom BrowserField

Forget to add image

As you can see, election1 image can't be display.

Please use plain text.
Contributor
yincan
Posts: 19
Registered: ‎01-18-2011
My Device: Bold 9000
My Carrier: at&t

Re: Display html image on Custom BrowserField

I think for the request image, you need to start another thread to fetch from the image's url, like the demo has the second thread to do that. 

 

ah, why not try the BrowserField, and add the setResourceRequestHandler, like this

 

 __bf = new BrowserField(config);
        ProtocolController controller = new ProtocolController(__bf);
        config.setProperty(BrowserFieldConfig.CONTROLLER, controller);
        controller.setNavigationRequestHandler("http", new BrowserFieldNavigationRequestHandler() {
            public void handleNavigation(BrowserFieldRequest request) throws Exception {
                Logger.debug("handleNavigation " + request.getURL());
                BrowserSession bs = BrowserSessionFactory.getBroswerSession();
                bs.displayPage(request.getURL());
            }
        });
        controller.setResourceRequestHandler("http", new BrowserFieldResourceRequestHandler() {
            public InputConnection handleResource(BrowserFieldRequest request) throws Exception {
                if (request.getURL().startsWith("local:///")) {
                    return new BrowserFieldResponse(request.getURL(), ArticleScreen.class.getResourceAsStream(request
                            .getURL().substring("local:///".length())), "image/png");
                }
                return (InputConnection) Connector.open(request.getURL() + BaseFeedParser.connSuffix);
            }
        });

        __bf.displayContent(new HttpConnectionImpl(formatHtmlFromEncoded()), "");
        add(__bf);

 

 

 

Please use plain text.
Regular Contributor
gillaraz
Posts: 69
Registered: ‎10-19-2010
My Device: BB8520

Re: Display html image on Custom BrowserField

Yes, I heard that but The point is How to fetch from the image's url and add to the same field ?

 

Because I put all html data that contain <img src=http://www.xxx.com/sss.jpg> to the  ResourceFetchThread.

Please use plain text.
Regular Contributor
gillaraz
Posts: 69
Registered: ‎10-19-2010
My Device: BB8520

Re: Display html image on Custom BrowserField

Here is my code, I tried to use secondaryThread but it's not work.

 

Could someone help me about this ?

 

 

public class NewsDetail extends MainScreen implements RenderingApplication {

        // This method is use to parse html to show in Field
	
public void parseHTML(String data) { this.data = data; renderingSession.getRenderingOptions().setProperty( RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.JAVASCRIPT_ENABLED, true); renderingSession.getRenderingOptions().setProperty( RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.SHOW_IMAGES_IN_HTML, true); renderingSession.getRenderingOptions().setProperty( RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.ENABLE_CSS, true); renderingSession.getRenderingOptions().setProperty( RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.ENABLE_EMBEDDED_RICH_CONTENT, true); // RenderingOptions.ENABLE_EMBEDDED_RICH_CONTENT Font defaultFont = Font.getDefault().derive(Font.BOLD, 8); renderingSession.getRenderingOptions().setProperty( RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.DEFAULT_FONT_FACE, defaultFont.getFontFamily().getName()); renderingSession.getRenderingOptions().setProperty( RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.SHOW_IMAGE_PLACEHOLDERS, true); ResourceFetchThread thread = new ResourceFetchThread(data, null, this); thread.start(); } public void processConnection(HttpConnection connection, Event e) { // cancel previous request if (currentConnection != null) { try { currentConnection.close(); } catch (IOException e1) { } } currentConnection = connection; BrowserContent browserContent = null; try { browserContent = RenderingSession.getNewInstance().getBrowserContent(connection, this, e); if (browserContent != null) { field = browserContent.getDisplayableContent(); if (field != null) { synchronized (Application.getEventLock()) { //this.mainScreen.add(field); this.x.add(field); //BrowserScreen.mainScreen.add(field); //UiApplication.getUiApplication().pushScreen(mainScreen); } } browserContent.finishLoading(); SecondaryResourceFetchThread.doneAddingImages(); } } catch (RenderingException re) { System.out.println("RenderingException : " + re); } catch (Exception ex) { System.out.println("Exception : " + ex); ex.printStackTrace(); } } public Field getField(){ return field; } public Object eventOccurred(Event event) { int eventId = event.getUID(); switch (eventId) { case Event.EVENT_URL_REQUESTED: { ResourceFetchThread thread = new ResourceFetchThread(data, null, this); thread.start(); break; } case Event.EVENT_BROWSER_CONTENT_CHANGED: { break; } case Event.EVENT_REDIRECT: { RedirectEvent e = (RedirectEvent) event; String referrer = e.getSourceURL(); switch (e.getType()) { case RedirectEvent.TYPE_SINGLE_FRAME_REDIRECT: // show redirect message Application.getApplication().invokeAndWait(new Runnable() { public void run() { Status.show("You are being redirected to a different page..."); } }); break; case RedirectEvent.TYPE_JAVASCRIPT&colon;; break; case RedirectEvent.TYPE_META: // MSIE and Mozilla don't send a Referer for META Refresh. referrer = null; break; case RedirectEvent.TYPE_300_REDIRECT: // MSIE, Mozilla, and Opera all send the original // request's Referer as the Referer for the new // request. Object eventSource = e.getSource(); if (eventSource instanceof HttpConnection) { referrer = ((HttpConnection) eventSource).getRequestProperty(REFERER); } break; } HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setProperty(REFERER, referrer); //requestHeaders.setProperty("Content-Language","th"); // requestHeaders.setProperty("Accept-Language","th"); //requestHeaders.setProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7"); ResourceFetchThread thread = new ResourceFetchThread(this.data, event, this); thread.start(); break; } case Event.EVENT_CLOSE: // TODO: close the appication break; case Event.EVENT_SET_HEADER: // no cache support case Event.EVENT_SET_HTTP_COOKIE: // no cookie support case Event.EVENT_HISTORY: // no history support case Event.EVENT_EXECUTING_SCRIPT: // no progress bar is supported case Event.EVENT_FULL_WINDOW: // no full window support case Event.EVENT_STOP: // no stop loading support default: } return null; } /** * @see */ public int getAvailableHeight(BrowserContent browserField) { // field has full screen return Graphics.getScreenHeight(); } /** * @see */ public int getAvailableWidth(BrowserContent browserField) { // field has full screen return Graphics.getScreenWidth(); } /** * @see */ public int getHistoryPosition(BrowserContent browserField) { // no history support return 0; } public String getHTTPCookie(String url) { // no cookie support return null; } public HttpConnection getResource(RequestedResource resource, BrowserContent referrer) { if (resource == null) { return null; } // check if this is cache-only request if (resource.isCacheOnly()) { // no cache support return null; } String url = resource.getUrl(); if (url == null) { return null; } // if referrer is null we must return the connection if (referrer == null) { // HttpConnection connection = Utilities.makeConnection(resource.getUrl(), resource.getRequestHeaders(), null); HttpConnection connection = new HttpConnectionImpl(url); return connection; }else { // if referrer is provided we can set up the connection on a separate thread SecondaryResourceFetchThread.enqueue(resource, referrer); } return null; } /** * @see */ public void invokeRunnable(Runnable runnable) { (new Thread(runnable)).run(); } public Field getDisplayableField() { if (displayableField == null) displayableField = new NullField(); return displayableField; } } class ResourceFetchThread extends Thread { private NewsDetail application; private Event event; private String data; ResourceFetchThread(String data, Event event, NewsDetail application) { this.data = data; this.application = application; this.event = event; } public void run() { HttpConnection connection = new HttpConnectionImpl(this.data); this.application.processConnection(connection, this.event); } }

 

class SecondaryResourceFetchThread extends Thread {

    /**
     * Callback browser field
     */
    private BrowserContent    _browserField;

    /**
     * Images to retrieve
     */
    private Vector            _imageQueue;

    /**
     * True is all images have been enqueued
     */
    private boolean           _done;

    /**
     * Sync object
     */
    private static Object     _syncObject = new Object();

    /**
     * Secondary thread
     */
    private static SecondaryResourceFetchThread _currentThread;

    /**
     * Enqueues secondary resource for a browser field.
     *
     * @param resource - resource to retrieve
     * @param referrer - call back browsr field
     */
    public static void enqueue(RequestedResource resource, BrowserContent referrer) {

        if (resource == null) {
            return;
        }

        synchronized( _syncObject ) {

            // create new thread
            if (_currentThread == null) {
                _currentThread = new SecondaryResourceFetchThread();
                _currentThread.start();
            } else {
                // if thread alread is running, check that we are adding images for the same browser field
                if (referrer != _currentThread._browserField) {
                    synchronized( _currentThread._imageQueue) {
                        // if the request is for a different browser field,
                        // clear old elements
                        _currentThread._imageQueue.removeAllElements();
                    }
                }
            }

            synchronized( _currentThread._imageQueue) {
                _currentThread._imageQueue.addElement(resource);
            }

            _currentThread._browserField = referrer;
        }
    }

    /**
     * Constructor
     *
     */
    private SecondaryResourceFetchThread() {
        _imageQueue = new Vector();
    }

    /**
     * Indicate that all images have been enqueued for this browser field
     */
    public static void doneAddingImages() {
        synchronized( _syncObject ) {
            if (_currentThread != null) {
                _currentThread._done = true;
            }
        }
    }

    public void run() {

        while (true) {

            if (_done) {

                // check if we are done requesting images
                synchronized( _syncObject ) {
                    synchronized( _imageQueue ) {
                        if (_imageQueue.size() == 0) {
                            _currentThread = null;
                            break;
                        }
                    }

                }
            }

            RequestedResource resource = null;

            // request next image
            synchronized( _imageQueue ) {
                if (_imageQueue.size() > 0) {
                    resource = (RequestedResource)_imageQueue.elementAt(0);
                    _imageQueue.removeElementAt(0);
                }
            }

            if (resource != null) {

                HttpConnection connection = new HttpConnectionImpl(_syncObject+"");
                	
                	//Utilities.makeConnection(resource.getUrl(), resource.getRequestHeaders(), null);
                resource.setHttpConnection(connection);
                
                // signal to the browser field that resource is ready
                if (_browserField != null) {
                    _browserField.resourceReady(resource);
                }
            }

        }
    }

}

 

 

Please use plain text.
New Contributor
daali
Posts: 4
Registered: ‎07-09-2010
My Device: Storm 9550
My Carrier: Verizon

Re: Display html image on Custom BrowserField

Did anyone ever figure this out?

Please use plain text.