user572627
user572627

Reputation: 1

How do I set the SDCard path in a Blackberry simulator, and how can I read files from the SDCard using the FileConnection API?

My Blackberry application should read Images which is stored in a SD card.

I have to set a path for the SD Card in the Blackberry simulator so that I can read the image using the FileConnection APIs.

Can anyone give me the solution?

Upvotes: 0

Views: 3614

Answers (2)

iKushal
iKushal

Reputation: 2869

1.create folder and give name-SDCard.
2.in the simulator click on-simulate.
3.choose change SD Card.
4.select  your folder SDCard.
5.click on close.

now create file connection
FileConnection fileConnection = (FileConnection)Connector.open(("file:///SDCard/images/a.png") 
 ,Connector.READ, true);
InputStream inputStream =  fileConnection.openInputStream();
byte[] imageBytes = new byte[(int) fileConnection.fileSize()];
inputStream.read(imageBytes);
inputStream.close();
EncodedImage eimg = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);


now u can use this encoded image any where.

Upvotes: 3

Ashraf Bashir
Ashraf Bashir

Reputation: 9804

If you mean you need to set the simulator path for the SD card,
here are the steps how you do this in eclipse:
1- Run the simulator
2- Choose "Smulate"
3- Choose "Change SD Card"
4- Press "Add Directory"
5- Browse and press "OK"


But if you need the code to open the images here it is:

FileConnectionApplication.java:

public class FileConnectionApplication extends UiApplication {
    public FileConnectionApplication() {
        FileConnectionScreen screen = new FileConnectionScreen();
        pushScreen(screen);
    }

    public static void main(String[] args) {
        FileConnectionApplication app = new FileConnectionApplication();
        app.enterEventDispatcher();
    }
}


FileConnectionScreen.java:

public class FileConnectionScreen extends MainScreen {
    private ObjectListField fileList;
    private String currentPath = "file:///";

    public FileConnectionScreen() {
        setTitle("FileConnection");
        fileList = new ObjectListField();
        fileList.set(new String[] { "store/", "SDCard/" });
        add(fileList);
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem("Select", 10, 10) {
            public void run() {
                loadFile();
            }
        });
    }

    private void loadFile() {
        currentPath += fileList.get(fileList, fileList.getSelectedIndex());
        try {
            FileConnection fileConnection = (FileConnection) Connector.open(currentPath);
            if (fileConnection.isDirectory()) {
                Enumeration directoryEnumerator = fileConnection.list();
                Vector contentVector = new Vector();
                while (directoryEnumerator.hasMoreElements())
                      contentVector.addElement(directoryEnumerator.nextElement());
                String[] directoryContents = new String[contentVector.size()];
                contentVector.copyInto(directoryContents);
                fileList.set(directoryContents);
            } else if (currentPath.toLowerCase().endsWith(".jpg") || currentPath.toLowerCase().endsWith(".png")) {
                InputStream inputStream = fileConnection.openInputStream();
                byte[] imageBytes = new byte[(int) fileConnection.fileSize()];
                inputStream.read(imageBytes);
                inputStream.close();
                EncodedImage eimg = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
                UiApplication.getUiApplication().pushScreen(new ImageDisplayScreen(eimg));
            }
        } catch (IOException ex) {
        }
    }
}


ImageDisplayScreen.java:

public class ImageDisplayScreen extends MainScreen {
    public ImageDisplayScreen(EncodedImage image) {
        int displayWidth = Fixed32.toFP(Display.getWidth());
        int imageWidth = Fixed32.toFP(image.getWidth());
        int scalingFactor = Fixed32.div(imageWidth, displayWidth);
        EncodedImage scaledImage = image.scaleImage32(scalingFactor, scalingFactor);
        BitmapField bitmapField = new BitmapField();
        bitmapField.setImage(scaledImage);
        add(bitmapField);
    }
}

Upvotes: 1

Related Questions