【问题标题】:How do I set the SDCard path in a Blackberry simulator, and how can I read files from the SDCard using the FileConnection API?如何在黑莓模拟器中设置 SDCard 路径,以及如何使用 FileConnection API 从 SDCard 读取文件?
【发布时间】:2012-01-06 12:26:57
【问题描述】:

我的 Blackberry 应用程序应该读取存储在 SD 卡中的 Images

我必须在 Blackberry 模拟器中设置 SD 卡的路径,以便我可以使用 FileConnection API 读取图像。

谁能给我解决办法?

【问题讨论】:

  • 您只想设置路径吗?或者你还想用 sn-p 代码来读取 SD 卡图像文件?

标签: blackberry-simulator


【解决方案1】:
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.

【讨论】:

    【解决方案2】:

    如果您的意思是需要为 SD 卡设置模拟器路径,
    以下是在 eclipse 中执行此操作的步骤:
    1- 运行模拟器
    2- 选择“模拟”
    3- 选择“更换 SD 卡”
    4- 按“添加目录”
    5- 浏览并按“确定”


    但是,如果您需要代码来打开图像,它是:

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-16
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多