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

Java Development

Reply
Developer
mjnip
Posts: 27
Registered: ‎09-04-2008
My Device: Not Specified

Writing file to SD Card

I am having issues writing a file to the SD Card within JDE 4.6.  This is my code snippet, it throws an error at this line:

 

OutputStream lo_OS = lo_FileConn.openOutputStream();

 

The error has a null description, but the errorcode is 1005.  I haven't been able to dig up any info about a 1005.

I have verified that the txt file is created on the file system, but I obviously need to write into that file now. 

 

lo_MediaBytes = new byte[ lo_Bytes.length() ]; lo_MediaBytes = lo_Bytes.toString().getBytes(); try { FileConnection lo_FileConn = (FileConnection) Connector.open( "file:///SDCard/BlackBerry/uploadedfile.txt", Connector.READ_WRITE ); if ( !lo_FileConn.exists() ) { //create the file first lo_FileConn.create(); } lo_FileConn.setWritable(true); OutputStream lo_OS = lo_FileConn.openOutputStream(); lo_OS.write(lo_MediaBytes); } catch(IOException e) { int save = 12;

}

 

BlackBerry Development Advisor
MSohm
Posts: 14,855
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: Writing file to SD Card

Are you using a micro SD card image or the PC file system option?  They are outlined here:

 

How To - Use a computer file system as a microSD card
Article Number: DB-00654

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800792/801083/How_To_...

 

Are you working with a newly created file or does the file already exist?  Are any other applications referencing the file at the same time?

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
mjnip
Posts: 27
Registered: ‎09-04-2008
My Device: Not Specified

Re: Writing file to SD Card

I am using the pc file system option and already followed the directions outlined in that knowledge center article. 

 

I've tried using both a newly created file and a file that already exists.  No other applications reference file.

 

When I use a newly created file, the file is created as I can see it in my simulator SD card folder, I just can't open an output stream to it.  I can open an inputstream with no issue.  I've found a fortran Error 1005 which says: 

sequential-access I/O on direct-access unit

I looked through all the JDE examples and couldn't find any example of writing a file.  Is there one that I am missing?

 

Developer
mjnip
Posts: 27
Registered: ‎09-04-2008
My Device: Not Specified

Re: Writing file to SD Card

I've done more work on this and as much as I don't like to think it's the developer tools, I think this is an error with 4.6.  If you take this class and build it in 4.2 and run it on an 8800 simulator, it works.  Then take the built cod files, copy them to the 9000 sim directory and run it in the 9000 simulator, this works as well.  However, once you build the code with the 4.6 JDE, it gives a runtime error as what I originally posted at this line:

OutputStream lo_OS = lo_FileConn.openOutputStream();

In this example, I'm just reading in a file from a http connection, but you can read one from a resource and I'm sure the result will be the same.

 

I'm not sure how else to write to a file.  What I am trying to do is read a movie file from an http inputstream and write that movie clip to the filesystem.  As you can imagine, this is quite inhibiting because my target audience are Bold's (ie. 3G).

 

package ey.org.filetest;

import java.io.InputStream;
import java.io.*;

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

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;



public class FileTest extends UiApplication
{
    byte[] lo_MediaBytes                        = null;
    
    
    public static void main(String[] args)
    {
                FileTest theApp = new FileTest();
        theApp.enterEventDispatcher();
    }
        
        public FileTest()
        {
            
            MainScreen ms = new MainScreen(){
        
            }; 
            
                StreamConnection        lo_Conn         = null;
        InputStream             lo_Strm         = null;
        StringBuffer            lo_Bytes        = new StringBuffer();
        try
        {
                lo_Conn         = ( StreamConnection ) Connector.open( "http://toronipma/bbmedia/flower.jpg" ); //"http://toronipma/bbmedia/test.txt" );//
                lo_Strm         = lo_Conn.openInputStream();
                
                int ch;
                while ( ( ch = lo_Strm.read() ) != -1 )
                {
                        lo_Bytes.append( (char) ch );
                }
        }
        catch( Exception e )
        {
                //catch         
        }
        finally  //always close the connection
        {
                try
                {
                        if ( lo_Strm != null )
                        {
                                lo_Strm.close();
                        }
                        if ( lo_Conn != null )
                        {
                                lo_Conn.close();
                        }
                }
                catch( Exception e )
                {
                        
                }
        }
        
        //check that the image was received
        if ( lo_Bytes != null )
        {
                lo_MediaBytes           = new byte[ lo_Bytes.length() ];
                lo_MediaBytes           = lo_Bytes.toString().getBytes();       
                
                try 
                {
                        FileConnection lo_FileConn = (FileConnection) Connector.open( "file:///store/home/user/music/flower.jpg", Connector.READ_WRITE );
                        
                        if ( !lo_FileConn.exists() )
                        {
                                //create the file first
                                lo_FileConn.create();  
                        }
                        lo_FileConn.setWritable(true);                                  
                                OutputStream lo_OS = lo_FileConn.openOutputStream();
                                lo_OS.write(lo_MediaBytes);
                                lo_OS.close();
                }
                catch(IOException e)
                {                
                        int save = 12;
                }                               
        }
        
        this.pushScreen(ms);
        }

}

 

BlackBerry Development Advisor
MSohm
Posts: 14,855
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: Writing file to SD Card

I tried another similar application that writes out text to a file in the (non beta) release of the version 4.6.0 BlackBerry JDE and it did write to the newly created file without any exception.

 

Are you using the same version?  Please try pointing the simulated micro SD card to another location on your PC.  This could be a Windows file permission issue.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
mjnip
Posts: 27
Registered: ‎09-04-2008
My Device: Not Specified

Re: Writing file to SD Card

If you look at my code above I am no longer writing it to the SD Card, but to Device Memory. 

 

I am using JDE 4.6.0.12.

 

Please note what I stated above.  If you build in 4.2, and run in the 9000 simulator it works.  If you build in 4.6, and run in the 9000 simulator, it doesn't work.

 

Was your sample code built in 4.6.0 and if it was, can you please send me your working sample of code please?

BlackBerry Development Advisor
MSohm
Posts: 14,855
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: Writing file to SD Card

I tried writing to the store as well and didn't run into an exception.  Is the file being created or is that part failing too?  Have you tried running clean.bat in the BlackBerry simulator directory and retesting?

 

I was also using the version 4.6.0 Blackberry JDE.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
mjnip
Posts: 27
Registered: ‎09-04-2008
My Device: Not Specified

Re: Writing file to SD Card

I ran cleaned.bat and tried again.. Same issue.

 

The file is created fine, I just can't open a OutputStream to it.  This is so basic, I have no idea why it doesn't work.  I am completely puzzled.  This exact same code I posted above runs fine in JDE 4.2.

 

What version of java do you have? 

 

 

BlackBerry Development Advisor
MSohm
Posts: 14,855
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: Writing file to SD Card

I am using JDK version 1.6 with the version 4.6.0 BlackBerry JDE, however the BlackBerry Simulator does not rely on any JRE/JDK.

 

Are you able to simulate taking a picture using the BlackBerry simulator and save it to the simulated micro SD card?

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
avolcoff
Posts: 77
Registered: ‎08-01-2008
My Device: Not Specified

Re: Writing file to SD Card

does anyone have a similar example that saves an image file as gif or jpg?