Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Sep 2007
    Barranquilla - Colombia

    Unhappy Trying to read a file into a byte array

    i am using the following piece of code to read a file ( .tmm extension ) from the internal storage of my mobile device ( nokia 1680)

    InputStream tmmFile = getClass().getResourceAsStream(path);
    byte[] tmmData = new byte[tmmFile.available()];
    path = "file:///C:/predefgallery/predefphotos/FILENAME.tmm"

    In the 2nd line of the code shown above, an error is prompted
    Uncaught exception java/lang/NullPointerException.

    The file exists in this path ..... and i don't have any other path to store this file - i uploaded it thru MobiMB ( Mobile Media Browser )

    Am i doing something (or all ) wrong? =0)

    Looking forward to receive your views from above,

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Cheshire, UK

    Re: Trying to read a file into a byte array

    Class.getResourceAsStream() does not operate on a URL. The argument must be the name of a file in the application's JAR file.

    To access a file in the phone's file system, you need to use the FileConnection API. Note that this API is subject to a variety of security restrictions, including restricted access to certain parts of the file system, and security prompts to the user for access permission.

    Useful wiki pages:

    * File Connection API (JSR 75)

    * How to Read an Image from the Gallery in Java ME


  3. #3
    Registered User
    Join Date
    Sep 2007
    Barranquilla - Colombia

    Re: Trying to read a file into a byte array

    Thanx graham for your kind answer.
    If it works, i'll post the entire code.
    The purpose is to send a file from the mobile device to a webserver.
    This is just the part 1 of the entire task =0) the part 2 is to receive the bytearray in ASP.NET and store it in the server's filesystem.

    So far, very thank you.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Barranquilla - Colombia

    Talking Re: Trying to read a file into a byte array

    Thanx Graham! with your help, my code works! The file is read from the internal storage and is uploaded to the webserver thru a ASP.NET .aspx page.

    This is the final code to read the file from the internal storage:
    public void EnviarFichero(String iPath) throws IOException{
    	String path =iPath;
    	Image img = null;
    	String sURL = "http://webserver/virtualfolder/upload.aspx";
    	HttpConnection httpConnection = null;
    	DataOutputStream os = null;
            String mensaje="";
    	//img = Image.createImage(path);
    	httpConnection = (HttpConnection) Connector.open(sURL, Connector.READ_WRITE, false);
    	httpConnection.setRequestProperty("Connection", "keep-alive");
            String photos = "fileconn.dir.photos";
            path = System.getProperty(photos) + iPath;
            try {
                FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
                if(!fc.exists()) {
                    System.out.println("Archivo no existe!");
                else {
                    int size = (int)fc.fileSize();
                    InputStream is = fc.openInputStream();
                    byte bytesFichero[] = new byte[size];
                    is.read(bytesFichero, 0, size);
                    httpConnection.setRequestProperty("Content-Type", "multipart/form-data");
                    httpConnection.setRequestProperty("Content-Length", bytesFichero.length + "");
                    os = httpConnection.openDataOutputStream();
                    // enviar el archivo
                    int index = 0;
                    int sizeMaximo = 1024;
                        System.out.println("write:" + index);
                            sizeMaximo = bytesFichero.length - index;
                        os.write(bytesFichero, index, sizeMaximo);
                    mensaje=iPath + " enviado.";
            } catch (IOException ioe) {
                mensaje="IOException: "+ioe.getMessage();
            } catch (IllegalArgumentException iae) {
                mensaje="IllegalArgumentException: "+iae.getMessage();
            Alert Alerta1 =  new Alert("Status", mensaje, null, AlertType.INFO);
    }//fin de public void EnviarFichero

    This is the code in the ASP.NET page "upload.aspx"

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Introducir aquí el código de usuario para inicializar la página
            If Not Page.IsPostBack Then
                Dim str As System.IO.Stream, strmContents As String
                Dim counter, strLen, strRead As Integer
                str = Request.InputStream ' Create a Stream object.strArr
                strLen = CInt(str.Length) ' Find number of bytes in stream.
                Dim strArr(strLen) As Byte  ' Create a byte array.
                strRead = str.Read(strArr, 0, strLen) ' Read stream into byte array.
                ' Create a file and write the byte data to a file.
                Dim oFileStream As System.IO.FileStream
                oFileStream = New System.IO.FileStream(Server.MapPath("_tmm/" & Date.Now.Ticks.ToString), System.IO.FileMode.Create)
                oFileStream.Write(strArr, 0, strArr.Length)
            End If
        End Sub

    I have two final questions:

    1) Is there a way to put the real name to the uploaded file? that is, in the j2me code, i sent the bytes, not the name. I had to assign a name to the uploaded file, based upon the Ticks property of the Date class (in ASP.NET ....) but i would like to assign the real name "as is" in the mobile internal storage.

    2) Is there a way to initialize the FileBrowser "control" to start in a predefined folder, in order to avoid the navigation thru the filesystem of the device ( every time i navigate thru the folder it prompts me the security restrictions and i hope you agree with me: IS ANNOYING ....!!! =0) - or at least just one question...?

    Thanx in advance and very thanx again for your kind help!

Similar Threads

  1. Uploading Snapshot byte array to server.
    By krem82 in forum Mobile Java Networking & Messaging & Security
    Replies: 28
    Last Post: 2009-03-13, 20:12
  2. Error with makesis.exe and creating the .pkg file
    By gramya87 in forum Symbian Tools & SDKs
    Replies: 7
    Last Post: 2007-10-10, 13:53
  3. Contacts fields order??
    By timatima in forum Symbian
    Replies: 1
    Last Post: 2007-06-08, 13:51
  4. read image as byte array and save that in to RMS.
    By ar_kan in forum Mobile Java Media (Graphics & Sounds)
    Replies: 6
    Last Post: 2004-02-22, 22:11
  5. Replies: 0
    Last Post: 2002-06-10, 12:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts