我正在使用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卡不保存图像)
谁能帮我这个..等待你的答复提前&感谢....
干得好。这是一个精心编写的问题,需要明确的调试问题。我会在明天尝试在9900模拟器上运行它。 – Nate
@Nate,谢谢。将等待您的回复... –