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將執行並啓動服務器。

第五步:現在運行模擬器

我希望這將有助於。