Monday, January 14, 2013

BlackBerry Waiting screen while Downloading Image from a web url

we can show a progress animation while there is a downloading process is going on. for this purpose  create a project with the name WaitScreen and a the main-screen  class with name StartScreen .java copy the below given code to it


package jitesh.waitscreen;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class StartScreen extends MainScreen implements FieldChangeListener {
private ButtonField button;
BitmapField fieldDemo;
WaitScreen msgs;

public StartScreen() {

HorizontalFieldManager hfm = new HorizontalFieldManager();

VerticalFieldManager vfm = new VerticalFieldManager();
button = new ButtonField("Go!", FIELD_HCENTER);
button.setChangeListener(this);
vfm.add(button);

hfm.add(vfm);

add(hfm);
Bitmap bitmapImage = Bitmap.getBitmapResource("icon.png");
fieldDemo = new BitmapField(bitmapImage);
add(fieldDemo);
}

public void getFile() {
msgs = new WaitScreen(this);
HttpConnector
.HttpGetStream(
"http://upload.wikimedia.org/wikipedia/en/9/92/Magnifier_Vista_Icon.png"+";deviceside=true",
msgs);
}

// you should implement this method to use callback data on the screen.
public void updateScreen(Bitmap bitmap) {
fieldDemo.setBitmap(bitmap);
}

public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
if (field == button) {
getFile();

}
}
}



make  WaitScreen.java for showing  progress animation while downloading image from web url.  copy the given below code to it


package jitesh.waitscreen;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class WaitScreen extends FullScreen implements ResponseCallback {
StartScreen startScreen;

public WaitScreen(StartScreen startScreen) {
super(new VerticalFieldManager(), Field.NON_FOCUSABLE);
setBackground(BackgroundFactory.createSolidTransparentBackground(
Color.BLUE, 50));
this.startScreen = startScreen;
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_HEIGHT);

VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_WIDTH | FIELD_VCENTER);

vfm.add(new ProgressAnimationField(
Bitmap.getBitmapResource("spinner2.png"), 6,
Field.FIELD_HCENTER));

hfm.add(vfm);

add(hfm);

UiApplication.getUiApplication().pushScreen(this);
}

public void callback(Bitmap bmpimage) {
startScreen.updateScreen(bmpimage);
UiApplication.getUiApplication().popScreen(this);
}


}

the spinner2.png can be downloaded from here


after this make HttpConnector.java with following code


package jitesh.waitscreen;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.UiApplication;

public class HttpConnector {
static HttpConnection httpConnection = null;
static DataOutputStream httpDataOutput = null;
static InputStream httpInput = null;
static int rc;

static Bitmap bitmp = null;
static public void HttpGetStream(final String fileToGet,
final ResponseCallback msgs) {
Thread t = new Thread(new Runnable() {
public void run() {
try {
httpConnection = (HttpConnection) Connector.open(fileToGet);
rc = httpConnection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
httpInput = httpConnection.openInputStream();
InputStream inp = httpInput;
byte[] b = IOUtilities.streamToBytes(inp);
final EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
UiApplication.getUiApplication().invokeLater(
new Runnable() {
public void run() {

msgs.callback(hai.getBitmap());
}
});;

} catch (Exception ex) {
UiApplication.getUiApplication().invokeLater(
new Runnable() {
public void run() {
Bitmap bitmapImage = Bitmap
.getBitmapResource("icon.png");

msgs.callback(bitmapImage);
}
});
} finally {
try {
if (httpInput != null)
httpInput.close();
if (httpDataOutput != null)
httpDataOutput.close();
if (httpConnection != null)
httpConnection.close();
} catch (Exception e) {
e.printStackTrace();

}
}



}
});
t.start();
}


}



the used interface is ResponseCallback.java with the code


package jitesh.waitscreen;

import net.rim.device.api.system.Bitmap;

public interface ResponseCallback {
    public void callback(Bitmap bmp);
}

again the important class ProgressAnimationField.java is as follows


package jitesh.waitscreen;

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;


public class ProgressAnimationField extends Field implements Runnable
{
    private Bitmap _bitmap;
    private int _numFrames;
    private int _frameWidth;
    private int _frameHeight;
   
    private int _currentFrame;
    private int _timerID = -1;
   
    private Application _application;
    private boolean _visible;
         
    public ProgressAnimationField( Bitmap bitmap, int numFrames, long style )
    {
        super( style | Field.NON_FOCUSABLE );
        _bitmap = bitmap;
        _numFrames = numFrames;
        _frameWidth = _bitmap.getWidth() / _numFrames;
        _frameHeight = _bitmap.getHeight();
       
        _application = Application.getApplication();
    }
   
    public void run()
    {
        if( _visible ) {
            invalidate();
        }
    }
   
    protected void layout( int width, int height )
    {
        setExtent( _frameWidth, _frameHeight );
    }
   
    protected void paint( Graphics g )
    {
        g.drawBitmap( 0, 0, _frameWidth, _frameHeight, _bitmap, _frameWidth * _currentFrame, 0 );
        _currentFrame++;
        if( _currentFrame >= _numFrames ) {
            _currentFrame = 0;
        }
    }
   
    protected void onDisplay()
    {
        super.onDisplay();
        _visible = true;
        if( _timerID == -1 ) {
            _timerID = _application.invokeLater( this, 200, true );
        }
    }
   
    protected void onUndisplay()
    {
        super.onUndisplay();
        _visible = false;
        if( _timerID != -1 ) {
            _application.cancelInvokeLater( _timerID );
            _timerID = -1;
        }
    }
}

the output should look like this







No comments:

Post a Comment