2012-01-27 65 views
2

我正在開發使用嵌入式瀏覽器顯示html內容的BlackBerry應用程序。
在火炬上測試應用程序我意識到,只有項目嵌入式圖像由瀏覽器顯示,而我在訪問存儲在可移動SD卡或內部文件系統中的圖像資源文件時遇到問題。
運行在Curve上的相同應用程序正確顯示所有圖像。
下面是代碼片段:BlackBerry嵌入式瀏覽器:使用HTML的圖像顯示

browser = new BrowserField(); 

    Strimg img_1 = "file:///store/home/user/yellow.png"; 
    Strimg img_2 = "file:///SDCard/green.png"; 
    Strimg img_3 = "file:///local/images/red.png"; 

String imgTag_1 = "<img src=\"" + img_1 + "\">"; // Stored on file system - Not displayed by Torch 
String imgTag_2 = "<img src=\"" + img_2 + "\">"; // Stored on SDCard - Not displayed by Torch 
String imgTag_3 = "<img src=\"" + img_3 + "\">"; // Embedded image 

String browserContent = "<html>" + imgTag_1 + imgTag_2 + imgTag_3 + "</html>"; 

byte[] contentBytes;   
try { 
    contentBytes = browserContent.getBytes("UTF-8"); 
    browser.displayContent(contentBytes, "text/html; charset=UTF-8", "http://mysite.com"); 
} catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
    browser.displayContent(browserContent, "http://mysite.com"); 
} 

aVerticalManager.add(browser); 


我使用JRE 5無論是曲線和火炬。 使用FileConnector訪問文件可以正常工作。

關於如何在火炬和曲線上顯示圖像的任何建議? 謝謝

回答

2

解決!
此後描述我是如何做的代碼:

browser = new BrowserField();  
String img_1 = "file:///store/home/user/yellow.png"; // File Sistem 

byte[] byt_1 = MyUtilClass.readBytesFromFile(img_1); 
char[] base64_1 = MyUtilClass.Base64.encode(byt_1); 

// Sample of generating HTML: <html><img src="data:image/bmp;base64,Axcfhhòjbnesohokòbw...."></img></html> 
String imgTag_1 = "<html><img src= \"data:image/bmp;base64,"+ String.valueOf(base64_1) + "\" ></img></html>"; 

byte[] contentBytes;  

contentBytes = imgTag_1.getBytes("UTF-8"); 

// Inject HTML in browser 
browser.displayContent(contentBytes, "text/html; charset=UTF-8", "http://mysite.com"); 


所以,這個工作在火炬很好,但我對曲線表現欠佳。我將根據設備類型對行爲進行專門化。

if (Curve == true) { 
// Use: <img src=file:///store/home/user/yellow.png> 
} else { 
// Use: <img src="data:image/bmp;base64,Axcfhhòjbnesohokòbw...."></img> 
} 


我知道這是一個解決辦法,但它的工程!