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
Posts: 85
Registered: ‎05-03-2010
My Device: Not Specified
Accepted Solution

Problem in opening a pdf url in a BrowserField of an application.

[ Edited ]

Device : 9530 (Simulator)

Device and App OS : 5.0

BES version : 4.1.7.18

Device Manager : 5.0.1.20

Desktop Manager: 5.0.1

 

 

I need to to open pdfs url my application . Initially I found out that a normal MDS simulator won't open a PDF in a BlackBerry Simulator

 

http://supportforums.blackberry.com/t5/Web-Development/Viewing-PDF-through-simulator/m-p/25503#M81

 

and that I need a BES for that, so I installed BES and after a lot of fooling around I got it to work. As of now, I can open the pdf url and all other urls in the native browser of my Blackberry Simulator. But when I try to open the pdf url from within an application using a browserField it does not work.

 

I've tried using the simple browserField sample provided with the API

http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/browser/field2/BrowserField.ht...

 

I've tried using a browserField example from the KB (out of desperation really, i do not know what it does)

http://supportforums.blackberry.com/t5/Java-Development/BrowserField-Sample-Code-Using-the-BrowserFi...

 

Even tried using the sample code given in the Apress Advanced BlackBerry Development by Chris King book. Here's the code for anyone who's interested in having a look at it

 

 

import java.io.IOException;
import java.util.*;

import javax.microedition.io.*;

import net.rim.device.api.browser.field.*;
import net.rim.device.api.io.http.HttpHeaders;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.*;

public class BrowserScreen extends MainScreen implements Runnable,
        RenderingApplication
{
    private RenderingSession renderSession;
    private LabelField status;
    private StatusUpdater updater;
    private String url;

    public BrowserScreen()
    {
        renderSession = RenderingSession.getNewInstance();
        status = new LabelField("Loading...");
        add(status);
        updater = new StatusUpdater(status);
        url = "http://www.google.com";
        (new Thread(this)).start();
    }

    private class BrowserFieldContainer extends VerticalFieldManager
    {
        public BrowserFieldContainer()
        {
            super(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR
                    | Manager.FIELD_HCENTER);
        }

        public void sublayout(int maxWidth, int maxHeight)
        {
            int width = BrowserScreen.this.getWidth();
            int height = BrowserScreen.this.getHeight();
            super.sublayout((int) (width * .9), height / 2);
        }
    }

    public void run()
    {
        HttpConnection conn = null;
        try
        {
            conn = (HttpConnection) Connector.open(url);
            updater.sendDelayedMessage("Connection opened");
            BrowserContent browserContent = renderSession.getBrowserContent(
                    conn, this, null);
            if (browserContent != null)
            {
                Field field = browserContent.getDisplayableContent();
                if (field != null)
                {
                    synchronized (Application.getEventLock())
                    {
                        deleteAll();
                        add(status);
                        add(new LabelField("Your search starts here."));
                        BrowserFieldContainer container = 
                            new BrowserFieldContainer();
                        container.add(field);
                        add(container);
                        add(new LabelField("Don't forget to tip the service!"));
                    }
                }
                browserContent.finishLoading();
            }
        }
        catch (Exception e)
        {
            updater.sendDelayedMessage(e.getMessage());
        }
        finally
        {
            try
            {
                if (conn != null)
                {
                    conn.close();
                }
            }
            catch (Exception e)
            {
            }
        }
    }

    public Object eventOccurred(Event event)
    {
        if (event.getUID() == Event.EVENT_URL_REQUESTED)
        {
            UrlRequestedEvent urlRequestedEvent = (UrlRequestedEvent) event;
            url = urlRequestedEvent.getURL();
            (new Thread(this)).start();
        }
        updater.sendDelayedMessage("Handle event " + event.getUID() + " for "
                + event.getSourceURL());
        return null;
    }

    public int getAvailableHeight(BrowserContent browserContent)
    {
        return getHeight() / 2;
    }

    public int getAvailableWidth(BrowserContent browserContent)
    {
        return (int) (getWidth() * .9);
    }

    public String getHTTPCookie(String url)
    {
        return null;
    }

    public int getHistoryPosition(BrowserContent browserContent)
    {
        return 0;
    }

protected HttpConnection getResourceConnection(String url,
        HttpHeaders requestHeaders)
{
    HttpConnection connection = null;
    try
    {
        connection = (HttpConnection) Connector.open(url);
        if (requestHeaders != null)
        {
            Hashtable headers = requestHeaders.toHashtable();
            if (headers != null)
            {
                Enumeration names = headers.keys();
                while (names.hasMoreElements())
                {
                    String name = (String) names.nextElement();
                    String value = (String) headers.get(name);
                    connection.setRequestProperty(name, value);
                }
            }
        }
    }
    catch (IOException ioe)
    {
        updater.sendDelayedMessage(ioe.getMessage());
    }
    return connection;
}

    public HttpConnection getResource(final RequestedResource resource,
            final BrowserContent referrer)
    {
        if (resource == null || resource.isCacheOnly())
        {
            return null;
        }

        String url = resource.getUrl();

        if (url == null)
        {
            return null;
        }

        if (referrer == null)
        {
            return getResourceConnection(resource.getUrl(), resource
                    .getRequestHeaders());
        }
        else
        {
            (new Thread()
            {
                public void run()
                {
                    HttpConnection connection = getResourceConnection(resource
                            .getUrl(), resource.getRequestHeaders());
                    resource.setHttpConnection(connection);
                    referrer.resourceReady(resource);
                }
            }).start();
        }
        return null;
    }

    public void invokeRunnable(Runnable runnable)
    {
        (new Thread(runnable)).start();
    }

}

 

 

I can open google.com and other pages from my application using the above given alternatives but cannot open a url pdf. And the pdf's open fine in the local BlackBerry Browser. I'm getting kinda desperate for help now. Appreciate any I can get.

 

Much Obliged.

 

 

 

 

Developer
Posts: 85
Registered: ‎05-03-2010
My Device: Not Specified

Re: Problem in opening a pdf url in a BrowserField of an application.

I guess i'll rephrase my question:

 

Is it even possible to display a pdf url programatically, using the browserField within an application??

BlackBerry Development Advisor
Posts: 15,125
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: Problem in opening a pdf url in a BrowserField of an application.

The browser field does not support the rendering of PDF files.  There are no APIs in the BlackBerry API set that allow you to programmatically display a PDF file other then by opening the BlackBerry Browser the the  URL of your PDF file, which must point to a web server.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 85
Registered: ‎05-03-2010
My Device: Not Specified

Re: Problem in opening a pdf url in a BrowserField of an application.

Thanks a lot Mark for the clarification. Although I'm a bit dissapointed, I am much relieved to know that it simply cannot be done and I need not waste my time on it.

 

Cheers.

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

Re: Problem in opening a pdf url in a BrowserField of an application.

Mark,

 

Now that OS 6 has been released, is there now any support to natively display PDFs on a BlackBerry? PDFs have been around for over a decade and I would hope that this functionality has eventually been added.

 

Thanks,

Daniel

 

 

BlackBerry Development Advisor
Posts: 15,125
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: Problem in opening a pdf url in a BrowserField of an application.

There are no APIs in the BlackBerry API set to render local PDFs within a third party application.  They can be viewed natively as email attachments or from within the browser (when accessed from a web server).

 

There are third party applications available that can display local PDFs.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: Problem in opening a pdf url in a BrowserField of an application.

What is the reason for the lack of native PDF support?

 

I am also alternatively experimenting with SVG and the performance to render a 600kb large SVG is rather poor too.

 

It's really disappointing not to have any decent native support for the most commonly used document formats :-(

 

 

Trusted Contributor
Posts: 201
Registered: ‎12-17-2010
My Device: Not Specified

Re: Problem in opening a pdf url in a BrowserField of an application.

Hi,

So it means we can not open .pdf  file programmatically using browserfield.

Am I right?

I have url to view pdf using browserfield ..

So is it possible or not ?

I tried a lot but it gave me error in browserfield.

 

Thanks,