我正在使用 HTTPConnections & Filesystems 下载图像并将该图像保存在黑莓模拟器 SDCard 中。当我执行代码时,它在 BB 9800 模拟器(操作系统版本 6.0)和 BB 9550 模拟器(操作系统版本 5.0)中运行良好。但是当我在 BB 9900 Simulator(操作系统版本 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();
}
}
下载Helper.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 SDCard 中的代码。
在黑莓模拟器中:
BB 9550 (5.0 OS) ---- 工作(在 SDCard 中保存图像)
BB 9800 (6.0 OS) ---- 工作(在 SDCard 中保存图像)
BB 9900 (7.1 OS) ---- 不工作(不保存图像在 SD 卡中)
任何人都可以帮我解决这个问题.. 等待您的回复并提前感谢....