Reputation: 1370
I am using HTTPConnections & Filesystems to download an image and saving that image in blackberry simulator SDCard. When i am executing the code it is working good in BB 9800 Simulator (OS Version 6.0) & in BB 9550 Simulator (OS Version 5.0) it is working. But when i executed the same code in BB 9900 Simulator (OS Version 7.1) not getting the output (i mean not saving the image in SDCard). The below is the following code i am using..
Code:
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.......");
}
}
The following is the code i am using to download image & save it in BB SDCard.
In Blackberry Simulators:
BB 9550 (5.0 OS) ---- Working (Saving image in SDCard)
BB 9800 (6.0 OS) ---- Working (Saving image in SDCard)
BB 9900 (7.1 OS) ---- Not Working (Not Saving image in SDCard)
Can anyone help me with this.. Waiting for your reply & Thanks in advance....
Upvotes: 2
Views: 773
Reputation: 11
I had the same issue, but i solved it the following way.
Step1: set java home path:
set JAVA_HOME=C:\Program Files\Java\jre6
Step2: select project - > click on run configuration -> select simulator tab -> tick on launch MDS connection service with simulator
Step3 : double click on rub.bat
file, located in MDS
, which is located where you install Eclipse in my system this folder located in
F:\eclipse\eclipse\plugins\net.rim.ejde.componentpack7.1.0_7.1.0.10\components\MDS
Step4: Double click on run.bat
file. cmd.exe will execute and start server.
Step5: now run simulator
I hope this will help.
Upvotes: 0
Reputation: 31045
I just ran the code on my 9900 OS 7.1 simulator, and it works for me. That doesn't mean the code is perfect, and can't fail under unusual scenarios. But, here is my guess:
Each simulator has individual settings. Did you remember to setup the SDCard for your 9900 simulator? On the simulator menu, go to Simulate -> Change SD Card ... and make sure the SDCard is setup. I normally use just one SDCard directory on my computer C:\temp\SDCard
so that I can run different simulators, and have the same /SDCard/BlackBerry/pictures/ directory, for example.
As I mentioned in the answer where I posted DownloadHelper
, the code assumes the folder /SDCard/BlackBerry/pictures exists. You might want to make your code create that folder with mkdirs()
, if it doesn't exist, or at least check the SDCard folder that your simulator is using, and make sure there's already a BlackBerry/pictures folder there.
Otherwise, just use the debugger, and try to determine which line of DownloadHelper.run()
is failing.
Upvotes: 1