Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
Posts: 67
Registered: ‎09-23-2010
My Device: Curve
My Carrier: T-Mobile
Accepted Solution

Help - java extension for binary file download

I was wondering if someone could look over the following and tell if I have made some simple mistake somewhere in the java code. I have written an extension to allow me to download a binary file by passing a URL for a web server and a file destination for onboard storage on the BB device.

 

It doesn't return any javascript errors, but it also doesnt download the file to the device.

 

I call it from javascript like this,  

 

csiFileIO.binImageLoader( imgName, filePath3 );

 

where imgName is the full URL to the png file on my webserver and filePath3 is the file location on the device.

 

And, yes the domain for the image is whitelisted in config.xml

 

Thanks in advance

 

package widgetpackage;

import org.w3c.dom.Document;

import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.script.ScriptEngine;
import net.rim.device.api.web.WidgetConfig;
import net.rim.device.api.web.WidgetExtension;

public final class csiFileIO implements WidgetExtension {

    public String[] getFeatureList() {
        
        String[] result = new String[1];
        result[0] = "csiFileIO";
        return result;
    }

    public void loadFeature(String feature, String version, Document doc,
            ScriptEngine scriptEngine) throws Exception {
        
        if (feature == "csiFileIO") {
            scriptEngine.addExtension("csiFileIO.binImageLoader", new binImageLoader());
            }

    }

    public void register(WidgetConfig arg0, BrowserField arg1) {
        // TODO Auto-generated method stub

    }

    public void unloadFeatures(Document arg0) {
        // TODO Auto-generated method stub

    }

}

 

 

 

package widgetpackage;

import net.rim.device.api.script.Scriptable;

public final class UpdateImage extends Scriptable {	

	public static final String FIELD_CHANGEICON = "binimageloader";
	private binImageLoader changer;
	
	public UpdateImage(){		
		this.changer = new binImageLoader();	
	}
	
	public Object getField(String name) throws Exception {					
			
		if (name.equals(FIELD_CHANGEICON)) 
		{
			return this.changer;
		}
		
			return super.getField(name);
	}

}

 

 

package widgetpackage;

import java.io.*;

import javax.microedition.io.Connector;
import javax.microedition.io.file.*;
import javax.microedition.io.HttpConnection;

import java.io.IOException;
import net.rim.device.api.io.File;
import net.rim.device.api.script.ScriptableFunction;

public class binImageLoader extends ScriptableFunction {
	String url;
	String fdest;
	public Object invoke(Object obj, Object[] args) throws Exception {
		if (args.length == 2) {      
			url   = ((String)args[0]).toString(); 
			fdest = ((String)args[1]).toString();      
			}
	if (url != null ) {
      HttpConnection c = null;
      InputStream is = null;
      int rc;

      try {
          c = (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.
          rc = c.getResponseCode();
          if (rc != HttpConnection.HTTP_OK) {
              throw new IOException("HTTP response code: " + rc);
          }

          is = c.openInputStream();
      
          // Get the length and process the data
          int len = (int)c.getLength();
          if (len > 0) {
               int actual = 0;
               int bytesread = 0 ;
               byte[] data = new byte[len];
               while ((bytesread != len) && (actual != -1)) {
                  actual = is.read(data, bytesread, len - bytesread);
                  bytesread += actual;
               }
          
               try {
            	     FileConnection fconn = (FileConnection)Connector.open(fdest);
            	     if (!fconn.exists()) {
            	         fconn.create();  // create the file if it doesn't exist
            	     	 DataOutputStream outstream;
            	     	 outstream =fconn.openDataOutputStream();
             	     	 outstream.write(data);
            	     	 outstream.flush();
            	     	 outstream.close();
            	     }
            	     	 fconn.close();
            	 }
            	 catch (IOException ioe) {
            	 }
               
          }
      } catch (Throwable t) 
      	{
      	}
      return UNDEFINED;
      }
	return obj;
    }
  }

 

Developer
Posts: 382
Registered: ‎05-19-2008
My Device: BlackBerry Torch
My Carrier: AT&T

Re: Help - java extension for binary file download

 

What does your javascript call return? Since your ScriptableFunction returns something (either UNDEFINED or the obj param) if you look at the return value it should provide some clue about what is happening.

I think though, that you aren't calling the right method from javascript. Try this instead.

csiFileIO.binImageLoader.binimageloader( imgName, filePath3 );

 

 In the widget extension, you define the extension to be the object binImageLoader

if (feature == "csiFileIO") {
    scriptEngine.addExtension("csiFileIO.binImageLoade​r", new binImageLoader());
}

 Later, in scriptable, you define a new field named 'binimageloader'

	public static final String FIELD_CHANGEICON = "binimageloader";
	...	
	public Object getField(String name) throws Exception {				if (name.equals(FIELD_CHANGEICON)) 
		{
			return this.changer;
		}
		return super.getField(name);
	}

 Hope that helps

Bill
-------------------------------------------
Check out my book on BlackBerry Development for Java.
And my other really really old book
My Apps: FlashKids
Developer
Posts: 67
Registered: ‎09-23-2010
My Device: Curve
My Carrier: T-Mobile

Re: Help - java extension for binary file download

Hey thanks for the suggestions. Using what you outlined I put in a few return statements in the error handlers (catch blocks) and was able to narrow it down to a 404 error from my source web server.

 

I was able to run the code included unmodified and with the method call as I had it in the first post against a second web server and it was successful.

 

Thanks again for the pointer and I bought your book - look forward to reading it.

Trusted Contributor
Posts: 227
Registered: ‎09-03-2010
My Device: Not Specified

Re: Help - java extension for binary file download

you could share of extension with me?