我正在开发一款使用嵌入式浏览器显示HTML内容的BlackBerry应用程序 在Torch上测试应用程序我已经意识到只有项目嵌入式图像是由浏览器显示的,而我在访问存储在可移动SD卡或内部文件系统上的图像资源文件时遇到了问题。 在曲线上运行的相同应用程序正确显示所有图像 这是一段代码:
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访问文件工作正常。
有关如何在Torch和Curve上显示图像的任何建议?
感谢
答案 0 :(得分: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");
所以,这在Torch上工作得很好,但我在Curve上表现不佳。我将根据设备类型对行为进行专门化。
if ( Curve == true ) {
// Use: <img src=file:///store/home/user/yellow.png>
} else {
// Use: <img src="data:image/bmp;base64,Axcfhhòjbnesohokòbw...."></img>
}
我知道这是一种解决方法,但它确实有效!