从服务器下载黑莓图像?

时间:2012-06-21 05:44:00

标签: blackberry blackberry-simulator httpconnection fileinputstream

我正在使用HTTPConnections&文件系统下载图像并在黑莓模拟器SDCard中保存该图像。当我执行代码时,它在BB 9800 Simulator(OS Version 6.0)&在BB 9550模拟器(OS版本5.0)它正在工作。但是当我在BB 9900模拟器(OS版本7.1)中执行相同的代码时没有获得输出(我的意思是不在SDCard中保存图像)。以下是我正在使用的以下代码..

代码:

MyApp.java

   public class MyApp extends UiApplication
 {
/**
 * Entry point for application
 * @param args Command line arguments (not used)
 */ 
   public static void main(String[] args)
  {
    // Create a new instance of the application and make the currently
    // running thread the application's event dispatch thread.
    MyApp theApp = new MyApp();       
    theApp.enterEventDispatcher();
  }
/**
 * Creates a new MyApp object
 */
   public MyApp()
  {        
    // Push a screen onto the UI stack for rendering.
    pushScreen(new MyScreen());
 }    
}

MyScreen.java

  public final class MyScreen extends MainScreen
 {
/**
 * Creates a new MyScreen object
 */
  public MyScreen()
  {        
    // Set the displayed title of the screen       
    setTitle("MyTitle");        
    LabelField title = new LabelField("hiiiiiiiiiiii", LabelField.ELLIPSIS);
    add(title);
    DownloadHelper downloader = new DownloadHelper("http://www.google.co.in/images/srpr/logo3w.png");
    System.out.println("this is downloader");
    Thread worker = new Thread(downloader);
    worker.start();     
   }
  }

DownloadHelper.java

  public class DownloadHelper implements Runnable{

private String _url;

   public DownloadHelper(String url) {
      _url = url;
   }
public void run() {
    // TODO Auto-generated method stub

    System.out.println("---------------download helper page");
    HttpConnection connection = null;
      OutputStream output = null;
      InputStream input = null;
      try {
         // Open a HTTP connection to the webserver
         connection = (HttpConnection) Connector.open(_url);
         // Getting the response code will open the connection, send the request,
         // and read the HTTP response headers. The headers are stored until requested.
         if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
             System.out.println("----------------http connection response");
            input = new DataInputStream(connection.openInputStream());
            int len = (int) connection.getLength();   // Get the content length
            if (len > 0) {
                System.out.println("--------------entered into condition");
               // Save the download as a local file, named the same as in the URL
               String filename = _url.substring(_url.lastIndexOf('/') + 1);
               FileConnection outputFile = (FileConnection) Connector.open("file:///SDCard/BlackBerry/pictures/" + filename, 
                     Connector.READ_WRITE);
               if (!outputFile.exists()) {
                  outputFile.create();
               }
               // This is probably not a robust check ...
               if (len <= outputFile.availableSize()) {
                  output = outputFile.openDataOutputStream();
                  // We'll read and write this many bytes at a time until complete
                  int maxRead = 1024;  
                  byte[] buffer = new byte[maxRead];
                  int bytesRead;

                  for (;;) {
                     bytesRead = input.read(buffer);
                     if (bytesRead <= 0) {
                        break;
                     }
                     output.write(buffer, 0, bytesRead);
                  }
                  output.close();
               }
            }
         }
      } catch (java.io.IOException ioe) {
         ioe.printStackTrace();
      } finally {
         try {
            if (output != null) {
               output.close();
            }
            if (connection != null) {
               connection.close();
            }
            if (input != null) {
               input.close();
            }
         } catch (IOException e) {
            // do nothing
         }
      }
      System.out.println("download completed.......");
}
}

以下是我用来下载图片和代码的代码。将其保存在BB SD卡中。

在黑莓模拟器中:

BB 9550(5.0 OS)----工作(在SD卡中保存图像)
   BB 9800(6.0 OS)----工作(在SDCard中保存图像)
    BB 9900(7.1操作系统)----无法正常工作(不在SD卡中保存图像)

任何人都可以帮助我..等待你的回复&amp;提前谢谢....

2 个答案:

答案 0 :(得分:1)

我刚刚在我的9900 OS 7.1模拟器上运行了代码,它对我有用。这并不意味着代码是完美的,并且在不寻常的情况下不会失败。但是,这是我的猜测:

每个模拟器都有各自的设置。您还记得为9900模拟器设置SDCard吗?在模拟器菜单上,转到模拟 - &gt; 更改SD卡...... 并确保已设置SD卡。我通常只在我的计算机C:\temp\SDCard上使用一个SDCard目录,这样我就可以运行不同的模拟器,并拥有相同的/ SDCard / BlackBerry / pictures /目录。

enter image description here

正如我在发布DownloadHelper的答案中所提到的,代码假设文件夹/ SDCard / BlackBerry / pictures存在。您可能想要创建代码create that folder with mkdirs()(如果它不存在),或者至少检查模拟器正在使用的SDCard文件夹,并确保已经有 BlackBerry / pictures 文件夹那里。

否则,只需使用调试器,并尝试确定哪一行DownloadHelper.run()失败。

答案 1 :(得分:0)

我遇到了同样的问题,但我通过以下方式解决了这个问题。

Step1:设置java home path:

set JAVA_HOME=C:\Program Files\Java\jre6

第2步:选择项目 - &gt;点击运行配置 - &gt;选择模拟器选项卡 - &gt;勾选启动带模拟器的MDS连接服务

第3步:双击位于rub.bat的{​​{1}}文件,该文件位于我的系统中安装Eclipse的位于

的文件夹中
MDS

Step4:双击F:\eclipse\eclipse\plugins\net.rim.ejde.componentpack7.1.0_7.1.0.10\components\MDS 文件。 cmd.exe将执行并启动服务器。

Step5:现在运行模拟器

我希望这会有所帮助。