This component lets user browse for pictures in his phone and select one
package mypackage; import java.io.IOException; import java.io.InputStream; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; import net.rim.device.api.system.Bitmap; import net.rim.device.api.system.DeviceInfo; import net.rim.device.api.system.EncodedImage; import net.rim.device.api.ui.Color; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.XYEdges; import net.rim.device.api.ui.component.BitmapField; import net.rim.device.api.ui.component.ButtonField; import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.ui.decor.Border; import net.rim.device.api.ui.decor.BorderFactory; import net.rim.device.api.ui.picker.FilePicker; public class MyBrowseScreen extends MainScreen implements FilePicker.Listener{ ButtonField browsePictures; private FilePicker _filePicker; BitmapField profilePictureField; Graphics graphics; Bitmap noImageBitmap = Bitmap.getBitmapResource("btn_discover.png");// any default bitmap to be displayed public MyBrowseScreen(){ _filePicker = FilePicker.getInstance(); _filePicker.setListener(this); profilePictureField = new BitmapField(noImageBitmap); profilePictureField.setBorder(BorderFactory.createRoundedBorder( new XYEdges(3, 3, 3, 3), Color.BLACK,100, Border.STYLE_FILLED)); add(profilePictureField); browsePictures = new ButtonField("Browse",100){ protected boolean navigationClick(int status, int time) { _filePicker.setTitle("Choose a picture"); _filePicker.setView(FilePicker.VIEW_PICTURES); _filePicker.show(); return super.navigationClick(status, time); } }; add(browsePictures); } public void selectionDone(String selected) { if (selected != null && selected.length() > 0) { FileConnection fc; try { fc = (FileConnection) Connector.open(selected); InputStream is = fc.openDataInputStream(); byte[] data_bytes = new byte[(int) fc.fileSize()]; is.read(data_bytes); EncodedImage en_image = EncodedImage.createEncodedImage(data_bytes, 0, -1); Bitmap picture = en_image.getBitmap(); // resize bitmap to bitmap field size picture = getResizedBitmap(noImageBitmap.getHeight(), noImageBitmap.getWidth(), en_image.getBitmap()); is.close(); fc.close(); // in torch devices the image from camera gallery comes inverted so we need to rotate it if(selected.indexOf("camera")!=-1 && Integer.parseInt(DeviceInfo.getDeviceName().substring(0, 4)) == 9800){ try { picture = rotateImage(picture, 90); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } profilePictureField.setBitmap(picture); invalidate(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void setBitmap(Bitmap profilePic){ profilePic = getResizedBitmap(noImageBitmap.getHeight(),noImageBitmap.getWidth(),profilePic); profilePictureField.setBitmap(profilePic); } public static Bitmap getResizedBitmap(int resizeHeight, int resizeWidth, Bitmap bitmap) { Bitmap scaleBitmap = new Bitmap(resizeWidth, resizeHeight); bitmap.scaleInto(scaleBitmap, Bitmap.FILTER_BILINEAR); return scaleBitmap; } public static Bitmap rotateImage(Bitmap image, int angle) throws Exception { if(angle == 0) { return image; } else if(angle != 180 && angle != 90 && angle != 270) { throw new Exception("Invalid angle"); } int width = image.getWidth(); int height = image.getHeight(); Bitmap rotated = new Bitmap(width, height); int[] rowData = new int[width]; int[] rotatedData = new int[width * height]; int rotatedIndex = 0; for(int i = 0; i < height; i++) { image.getARGB(rowData, 0, width, 0, i, width, 1); for(int j = 0; j < width; j++) { rotatedIndex = angle == 90 ? (height - i - 1) + j * height : (angle == 270 ? i + height * (width - j - 1) : width * height - (i * width + j) - 1 ); rotatedData[rotatedIndex] = rowData[j]; } } if(angle == 90 || angle == 270) { rotated.setARGB(rotatedData, 0, width, 0, 0, width, height); return rotated; } else { rotated.setARGB(rotatedData, 0, width, 0, 0, height, width); return rotated; } } }