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 Visitor
Posts: 1
Registered: ‎07-14-2011
My Device: Bold 9930
My Carrier: Sprint

Progress Bar while downloading a file

I am working on an application that will need to download a video file to the device and then start playing that file. I've got my video file playing correctly if I embed it in my application, but I am having some trouble making it download the file and show progress to the user while downloading.

 

This is inside my Screen constructor:

 

    view = new ProgressIndicatorView(0);
    model = new ProgressIndicatorModel(0, 100, 0);
    controller = new ProgressIndicatorController();   


    model.setController(controller);
    view.setModel(model);
    view.setController(controller);        
    controller.setModel(model);
    controller.setView(view);

    view.setLabel("Percent completion");
    view.createProgressBar(Field.FIELD_HCENTER);       

    add(view);

    UiApplication.getUiApplication().invokeLater(new Runnable()
    {
        public void run()
        {
            downloadVideo();
            initializeMedia();                

            // If initialization was successful...
            if(_videoField != null)
            {                    
                createUI();                   

                updateVideoSize();   

                try {
                    _player.start();

                } catch (MediaException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    ScreenSaverActivity.errorDialog("MEDIA EXCEPTION: "+e.toString());
                }
            }
            else
            {
                _statusField.setText("Error: Could not load media");
            }
        }
    });  

 This is my downloadVideo() method:

private void downloadVideo(){
    DownloadThread _dlThread = new DownloadThread();
    _dlThread.start();

}

 And this is the DownloadThread class:

class DownloadThread extends Thread {
    public void run(){

        try {
            HttpConnection httpConn;
            httpConn = (HttpConnection)Connector.open("http://url/to/myMovie.mp4");
            InputStream is = httpConn.openInputStream();

            String fName = "file:///store/BlackBerry/videos/myMovie.mp4";


            FileConnection fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
            if (!fconn.exists())
                fconn.create();

            OutputStream os = fconn.openOutputStream();
            long lengthOfFile = httpConn.getLength();
            int total = 0;
            int count;
            byte data[] = new byte[1024];
            while ((count = is.read(data)) != -1) {
                //I have tried this line without the 'synchronized' too, with no luck
                synchronized(Application.getEventLock()) {
                    EmbeddedMediaScreen.this.model.setValue((int)(total*100/lengthOfFile));
                }

                total += count;
              //write this chunk
                os.write(data, 0, count);
            }
            os.flush();
            os.close();
        } catch (IOException e) {
            ScreenSaverActivity.errorDialog(e.toString());
            e.printStackTrace();
        }
    }
}

I know that the download is working becauses if I let this run for a while and then check on the size of the file that it is storing I can see that it is getting larger. But the progress stays at 0 the entire time.

 

Before I even started trying with a progress bar I was trying to just update a RichTextField to show the numerical percentage of the download that was complete. I couldn't ever get  that TextField to reflect the changes after I called .setText() on it.

 

While my application is doing the download the device becomes unresponsive, so I am guessing that my trouble is the download work is happing on the main(UI) thread for some reason and that this has something to do with why I can't get the screen to update with the progress. Any help would be greatly appreciated.

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

Re: Progress Bar while downloading a file

Welcome to the forums.

 

I have not looked closely at you code, sorry.  However I suspect the problem occurs somewhere here:

 

            downloadVideo();
            initializeMedia(); 

 

You seem to correctly start a new Thread to do the download in downloadVideo, however I suspect that initializeMedia expects the file to be present.  naturally that can take some time.  SO you really need to divide your processing into two distinct parts, and have the completion of the download (which runs off the Event Thread) trigger some processing on the Event Thread.  That means you need to put the Ui into a holding state.  I find the easiest way to do this is to push a popup screen, make sure that the screen ignores all input (search for Splash Screen sample code as they have to do the same thing) and on this Screen, have your progress indicator. 

 

You might find this article of some use, though it is pretty hard going.  Note the comments too.

 

http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-screen-part-1/t...