2

我正在使用HTTPConnections &文件系统下载图像并将该图像保存在黑莓模拟器SDCard中。当我执行代码时,它在BB 9500 Simulator(OS Version 5.0)&中工作良好。但是,当我在BB 9900模拟器(操作系统版本7.1)中执行相同的代码没有得到输出(我的意思是不保存在SD卡中的图像)。下面的是下面的代码,我使用..从服务器下载黑莓图片?

代码:

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

以下ing是我用来下载图像的代码&将它保存在BB SDCard中。

在BlackBerry模拟器:

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

谁能帮我这个..等待你的答复提前&感谢....

+0

干得好。这是一个精心编写的问题,需要明确的调试问题。我会在明天尝试在9900模拟器上运行它。 – Nate

+0

@Nate,谢谢。将等待您的回复... –

回答

1

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

每个模拟器都有单独的设置。您是否记得为您的9900模拟器设置SDCard?在模拟器菜单上,转至模拟更改SD卡...并确保设置了SDCard。我通常只在我的电脑上使用一个SDCard目录C:\temp\SDCard,以便我可以运行不同的模拟器,并且具有相同的/ SDCard/BlackBerry/pictures /目录。

enter image description here

正如我在我贴DownloadHelper答案提到的,代码假设该文件夹/ SD卡/黑莓/图片存在。如果您的代码不存在,或者您至少需要检查您的模拟器正在使用的SDCard文件夹,并确保已经有一个BlackBerry /图片文件夹,那么您可能希望使其代码为create that folder with mkdirs()

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

+0

我会用调试器检查出来..... –

0

我有同样的问题,但我解决它以下面的方式。

第一步:组Java主路径:

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

第二步:选择项目 - >点击运行配置 - >选择模拟器选项卡 - >勾上推出MDS连接与模拟器服务

第3步:双击rub.bat文件,位于MDS,它位于您在我的系统中安装Eclipse的位置,该文​​件夹位于

F:\eclipse\eclipse\plugins\net.rim.ejde.componentpack7.1.0_7.1.0.10\components\MDS 

第四步:run.bat双击文件。 cmd.exe将执行并启动服务器。

第五步:现在运行模拟器

我希望这将有助于。