You Are Here:

Community: Blogs

Balagopal NKnair's Forum Nokia Blog

FileConnection - Introduction to beginners

balagopalks | 19 July, 2006 12:22

I have seen several of the new users looking for some help regarding Fileconnection (JSR 75). So thought of making these links as a blog, Hope this would be of some help to start developing.
 
Introduction To The FileConnection API (With Example) v1.1
 
Getting Started with the FileConnection APIs
 
An Overview of the File Connection Optional Package
 
The Generic Connection Framework
 
FileConnection API And PIM API
 
FileConnection API
 
Also I welcome others to post new interesting links/docs, which I might have missed, regarding Fileconnection.
 
Regards
Gopal

RSSComments

Re: FileConnection - Introduction to beginners

peterblazejewicz | 19/07/2006, 19:21

Hello Gopal,

That one from IBM dev-works was the first I've read following links from article about JSR-75 some time ago:
http://www-128.ibm.com/developerworks/edu/wi-dw-wi-navigate-i.html
IBM developer works existing account required (free),
it uses Sun WTK as reference test platform and covers all basic features (listing, file read operation),

regards,
Peter Blazejewicz

Re: FileConnection - Introduction to beginners

csIndia | 03/08/2006, 14:22

hi,

i tried to make simple application tht will make read contents of file & also write to tht same file the edited text. I am using carbide.j with eclipse, and using it for s60 emulator. But whn I insert fileConnection code in sm callback method, it gives Exception :

java.lang.securityexception : application not authorized to access the restricted API

can u tell me, how whould i go about solving it. I have tried for many days, and finding no solution, so asking you. Hope u wud help. Thanks.

-csIndia

Re: FileConnection - Introduction to beginners

balagopalks | 04/08/2006, 08:20

balagopalks Hi csInida,

Please note that this is Blogs section of Forum Nokia, it would be better if you post your question on the Forum Nokia Discussion boards http://discussion.forum.nokia.com/forum/index.php Mobile Java -> General topic. You could get free peer help from the fellow developers as well as by Nokia Experts and other Forum Nokia Champions.

Also you may go through this FAQ by Hartti.
FAQ (General issues) for Java discussion boards - read this before posting
http://discussion.forum.nokia.com/forum/showthread.php?t=85660&highlight=application+access+restricted+API

Regarding your question,
Please make sure that you have given proper permissions in your JAD and MF.

Two permissions are defined for the Fileconnection API:
javax.microedition.io.Connector.file.read
javax.microedition.io.Connector.file.write

The read permission is used when a file is open in read mode or if an InputStream is opened from a FileConnection object.
The write permission is used to open files in write mode or if an OutputStream is opened from a FileConnection. It is also used when invoking other write operations like delete or rename.

If you are not granted the permission to access the restricted API, a SecurityException is thrown and it is important to manage these situations in your MIDlet.

To avoid the security questions, you have to sign your midlet.

Also there have been several similar discussions in this regard. Please check this link
http://discussion.forum.nokia.com/forum/showthread.php?t=66360&highlight=restricted+API
I would suggest making a search in Forum Nokia Discussion Boards before making a post so that you may come across some quick answers.

Regarding Application Signing you may go through these links

MIDP 2.0: Tutorial On Signed MIDlets v1.1
http://www.forum.nokia.com/info/sw.nokia.com/id/88b8bf92-ab04-46b0-a07a-7c3e835e6d1e/MIDP_2_0_Tutorial_On_Signed_MIDlets_v1_1_en.pdf.html

http://discussion.forum.nokia.com/forum/showpost.php?p=210676&postcount=2
http://discussion.forum.nokia.com/forum/showthread.php?t=42411&highlight=signing

Hope this helps!

Regards
Gopal

Re: FileConnection - Introduction to beginners

csIndia | 04/08/2006, 09:12

Hi Gopal,

Thnx for the immediate response. Sorry to trouble you here. I wasn't aware where to post my query. I got your blog, and thought to post you as you seemed aware of it. After I posted question, came to know about the security settings, and immediately I was thru' wd it. Anyway, though, thnx for your time and the links are really helpful. :-)

Regards,

csIndia.

Re: FileConnection - Introduction to beginners

kadnan | 04/02/2007, 20:23

Hi Gopal

Pardon me If my question sounds irrelevent. I am intrested to knnow whether this API can access SMS on device and other folders like images/audio etc? Also can it also be helpful to use as Notification API which was provided by Microsoft in Windows Mobile 5.0?

thanks

/A

Re: FileConnection - Introduction to beginners

balagopalks | 05/02/2007, 17:35

balagopalks From a Java app you cannot access normal SMS's which was send to your mobile Inbox (incomming SMS) using Wireless Messaging API (JSR 120). You can access SMS which was send to a particular port and an application registers itself to listen for SMS messages sent to a specific port. When such message is received, the application is launched.

If the device supports JSR 75 - FileConnection, you can access different folders (that the device exposes) in the device.

Regarding accessing Inbox with Symbian C++, here is a similar discussion.
http://discussion.forum.nokia.com/forum/showthread.php?t=86515&highlight=SMS+inbox

Are you mentioning about State and Notification API (SNAPI) of Windows Mobile 5.0 ?. I am not much into SNAPI. Which feature of SNAPI are you talking about ?

To launch MIDlets automatically, have look at PushRegistry
http://discussion.forum.nokia.com/forum/showpost.php?p=278304&postcount=2

To make a call, you could use platformRequest()
http://developers.sun.com/techtopics/mobility/midp/ttips/platformRequest/

Hope this would be of some help!

Regards
Gopal

__________________
MobiQuil.com
An initiative by Developers for Developers.
http://www.mobiquil.com - Alpha - Launched.

Re: FileConnection - Introduction to beginners

susanta | 21/09/2007, 12:58

hi Gopal
Pardon me If my question disturbs you.I am working on NokiaMobileEmail_176x208 in which it has been asking to me to update the project by adding the property of file attachment.
How i can develope the application (in J2ME) in which user can upload or download files?
If it is possible then pls help me with coding pls.
Bye.

Re: FileConnection - Introduction to beginners

prats123 | 31/03/2008, 23:00

Hello sir,

I am the student of Information Technology Engg. Branch presently in
8th semester and doing my Major project "Compression and Decompression
in Mobile Phones". I have implemented LZW algorithm on mobile phone
.Problem what I am facing is to select the File to be compressed by
our project ie im building a file explorer , code is building and
running in emulators but not working in mobile .
It is just listing the roots but not listing the file and directories .

Please help me out and give me the right direction so that i can give
the path of the file implicitly. I am also sending u the code which i
had implemented

Regards,
Prateek

import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.file.*;
import javax.microedition.midlet.*;

public class MobileXplorer3 extends MIDlet implements CommandListener
{
// For our file system, this will be our root
private final static String ROOT = "/";
private String str1;
private String str;
// Definitions for directories
private final static String DIRECTORY_INDICATOR = "/";
private final static String UP_DIRECTORY_INDICATOR = "..";

// Holds the full path to the current directory. Point to the root at
// app startup
private String fullPath = ROOT;

// Our main display object.
// List of files/directories in the current directory
List lstDirectory = null;

// Icons for directory, file, and move-up-one directory
Image imgDirectory = null, imgFile = null, imgUpDirectory = null;

private Command cmExit; // Exit app
private Command cmSelect; // View file properties
private Command cmRead; // View file contents
private Command cmWrite; //Writing of viewed file
private Command cmBack; // Exit textbox showing file contents

/*--------------------------------------------------
* It all starts here. Check for FileConnection API
* get images, create commands and allocate List
*-------------------------------------------------*/
public MobileXplorer3()
{
if (System.getProperty("microedition.io.file.FileConnection.version")
== null)
{
System.out.println("FileConnection API not available");
destroyApp(false);
notifyDestroyed();
}
else
{
// Store references to our images for files and directories
try
{
imgFile = Image.createImage("/file.png");
imgDirectory = Image.createImage("/directory.png");
imgUpDirectory = Image.createImage("/up_directory.png");
System.out.println("Images is Readed");
}
catch(IOException e)
{ }
}

// Allocate the List that will hold directory contents
lstDirectory = new List(fullPath, List.IMPLICIT);

// Add commands and listen for events
cmExit = new Command("Exit", Command.EXIT, 1);
cmSelect = new Command("Select", Command.ITEM, 1);
cmRead = new Command("Read", Command.ITEM, 2);
cmBack = new Command("Back", Command.BACK, 1);
cmWrite = new Command("Write", Command.ITEM, 3);
lstDirectory.addCommand(cmExit);
lstDirectory.addCommand(cmSelect);
lstDirectory.addCommand(cmRead);
lstDirectory.setCommandListener(this);

// Default command when selecting an entry in the List
lstDirectory.setSelectCommand(cmSelect);

}

/*--------------------------------------------------
* Get list of roots and display the List object
*-------------------------------------------------*/
public void startApp()
{
getRootDirectories();
Display d= Display.getDisplay(this);
//SplashScreenTest st = new SplashScreenTest(this,d);

}
public void startMain()
{
// Create a list of root directories
getRootDirectories();
System.out.println("startApp is started");
// The List is our main displayable
Display.getDisplay(this).setCurrent(lstDirectory);
}
public void pauseApp()
{
}

public void destroyApp(boolean cond)
{
notifyDestroyed();
}

/*--------------------------------------------------
* Create a list of the valid root directories
*-------------------------------------------------*/
private void getRootDirectories()
{
// Get roots
Enumeration enum = FileSystemRegistry.listRoots();
System.out.println("Root directories ");
// Clear out the existing List contents
lstDirectory.deleteAll();

// Store entries in vector
while(enum.hasMoreElements())
{
String root = (String) enum.nextElement();
System.out.println(root);
lstDirectory.append(root, imgDirectory);
}
}

/*--------------------------------------------------
* Build list of files in the specified directory
*-------------------------------------------------*/
private void buildFileList(String dir)
{
String fname;
Enumeration enum;
FileConnection fc = null;

try
{
// Open connection to the specified directory
fc = (FileConnection) Connector.open("file://" + dir);

// Enumerate the list of returned files/directories
enum = fc.list("*", true);

// Clear out the existing List contents
lstDirectory.deleteAll();

// Show image that represents going up one directory level
lstDirectory.append("..", imgUpDirectory);

// Loop through all entries, building a List
while(enum.hasMoreElements())
{
fname = (String) enum.nextElement();

// Open connection
fc = (FileConnection) Connector.open("file://" + dir + "/" + fname);

// Append the name, along with an indicator to the List,
// specifying if the entry is a file or directory
lstDirectory.append(fname, fc.isDirectory() ? imgDirectory : imgFile);
}

fc.close();

}
catch (Exception e)
{ }
}

/*--------------------------------------------------
* Directory selected, change to the directory
*-------------------------------------------------*/
void changeToDirectory(String dirname)
{
// Selected ".." directory, move up the tree
if (dirname.equals(UP_DIRECTORY_INDICATOR))
{
// Locate the next to last separator so we can remove the path
char separator = fullPath.charAt(0);
int x = fullPath.lastIndexOf(separator, (fullPath.length() - 2));

// Remove the last path entry, as we are moving up the tree
fullPath = fullPath.substring(0, x + 1);
}
else // Drilling down the directory tree
{
// Update variable that holds the full directory path
fullPath += dirname;
}

// We worked our way up the tree back to the root,
// build list of roots
if (fullPath.length() == 1)
getRootDirectories();
else
// Build a list of files/dir given the new path
buildFileList(fullPath);

// Show the new List
Display.getDisplay(this).setCurrent(lstDirectory);
}

/*--------------------------------------------------
* File selected, show its properties
*-------------------------------------------------*/
void displayFileProperties(String fullPath, String filename)
{
try
{
FileConnection fc = (FileConnection) Connector.open(fullPath);

// Build an alert to show file properties
Alert fileinfo = new Alert(filename,
"Last Modified " + new Date(fc.lastModified()) + "\n" +
"Write access: " + (fc.canWrite() ? "yes" : "no") + "\n" +
"Read access: " + (fc.canRead() ? "yes" : "no") + "\n" +
"File size: " + fc.fileSize(),
null, AlertType.INFO);

// Wait for user acknowledgement
fileinfo.setTimeout(Alert.FOREVER);

// Show the alert
Display.getDisplay(this).setCurrent(fileinfo);

fc.close();
}
catch (IOException ioe)
{ }

}

/*--------------------------------------------------
* File selected, show its contents
* We assume the selected file contains pure text
*-------------------------------------------------*/
String displayFileContents(String fullPath, String filename)
{
// Textbox to hold the file contents, set it to read-only
TextBox tbx = new TextBox(filename, null, 2048,
TextField.UNEDITABLE | TextField.ANY);

// Create a byte array to hold file contents
byte[] b = new byte[2048];

// Connection and input stream
FileConnection fc = null;
InputStream is = null;
try
{
// Open file and stream
fc = (FileConnection) Connector.open(fullPath);
is = fc.openInputStream();
System.out.println(fullPath);
// Read no more than 2048 bytes
int len = is.read(b, 0, 2048);

// Here's how we get back from the file viewer textbox
tbx.addCommand(cmBack);
tbx.addCommand(cmWrite);
tbx.setCommandListener(this);

// Set textbox contents based on file read results...
if (len > 0)
{
// Place contents into the textbox
tbx.setString(new String(b, 0, len));
}
else
tbx.setString("Unable to read file!");
// Display the textbox and the file contents
Display.getDisplay(this).setCurrent(tbx);
str1 = tbx.getString();

is.close();
fc.close();
}
catch(Exception e)
{ }

return(str1);
}
/*--------------------------------------------------
* File selected for writing
* We assume the selected file contains pure text
*-------------------------------------------------*/
public void writing(String str1, String str)
{
System.out.println("Writing of data to be done");
System.out.println("Name of the File-"+" "+str1);
System.out.println("DATA TO BE WRITTEN-"+" "+str);
// Create a byte array to hold file contents
byte[] b = new byte[2048];
FileConnection fc = null;
OutputStream is = null;
System.out.println("Connection to be Establish");
try
{
// Open file and stream
fc = (FileConnection)
Connector.open("file:///root1/Compress/sample.txt",Connector.READ_WRITE);
System.out.println("Full path is - "+fullPath);
if (!fc.exists())
{
fc.create();
}
System.out.println("Connection is established");
OutputStream out = fc.openOutputStream();
String s="Dar ke aagaye JEEt hai ";
byte [] buf=s.getBytes();
byte [] buf1=str.getBytes();
out.write(buf);
out.write(buf1);

PrintStream output = new PrintStream(out);
output.println("this is a test");
System.out.println("WRITING has been Completed");
out.close();
fc.close();
}
catch(Exception e)
{ }
}

/*--------------------------------------------------
* Manage commands
*-------------------------------------------------*/
public void commandAction(Command c, Displayable s)
{
if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
else if (c == cmBack)
{
// Selected from the textbox showing a files contents,
// go back to our main list of files/directories
Display.getDisplay(this).setCurrent(lstDirectory);
}
else
{
// Get a reference to the selected List entry
String str = lstDirectory.getString(lstDirectory.getSelectedIndex());

// Select command...
if (c == cmSelect)
{
// Depending on whether a file or directory was selected...
// Valid directories are "/" or ".."
if(str.endsWith(DIRECTORY_INDICATOR) ||
str.equals(UP_DIRECTORY_INDICATOR))
changeToDirectory(str);
else
{
// Pass in the full path (including the selected file name)
// as well as the filename itself
displayFileProperties("file://" + fullPath + str, str);
}
}
else // Read file contents...
{
System.out.println("read file...");
displayFileContents("file://" + fullPath + str, str);

}
}
}
}
Reply
Forward

How to get the Application properties of installed software in mobile device

azar_sheik | 20/08/2008, 09:58

Hello everybody, Can anyone please tell me how to get the Application(installed Software) properties like(software version no,all the software details) using J2ME api if it is possible. i m trying to develop Midlet which should list all the softwares with details which installed in mobile device.

Thanks..

security option

Essam-ag | 06/09/2008, 23:02

Hello Mr. Balagopal Nknair

How are you?

I hope you are in good health.

thank you for your blog about file connection api, that I used it in my application.

And I buy certificate from Verisign but after I sign the midlet and install it to mobile it not work, and still confirm me to allow access to user data.
After I read and search for it, I found I have to modify it manually from application manager to let it always allow.

But that look hard to let user modify it spiceally it is in hidden place, so is there any way to automatically modify it while install application or after first start of the application.

Or maybe I wonder if I can set message while install application to confirm user to allow access for this application?

Hope you can help me in this issue

Thanks in advance for your help

Regards

Essam Al-Aghber

reading data from ByteOutputStreamArray

rohinanand | 10/10/2008, 11:32

Sir , I have written a code to save audio captured using microphone in an object of ByteOutputStreamArray. I want to do some real time signal processing on the data. can you please suggest me a method to read the data while it is being written on the array ..

reply asap

rohin

rohinanand.bme@gmail.com

Re: FileConnection - Introduction to beginners

Travel To United States | 28/09/2009, 02:37

I hope to know Nokia programming :( .

You must login to post comments. Login
 

Rate This

 
 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia