Mobile Application Developer, a former Forum Nokia Champion and now working as Technology Expert for Forum Nokia.
balagopalks | 19 July, 2006 12:22
Java |
Next |
Previous |
Comments (13) |
Trackbacks (0)
csIndia | 03/08/2006, 14:22
balagopalks | 04/08/2006, 08:20
Hi csInida,csIndia | 04/08/2006, 09:12
kadnan | 04/02/2007, 20:23
balagopalks | 05/02/2007, 17:35
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.susanta | 21/09/2007, 12:58
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
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..
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
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
Travel To United States | 28/09/2009, 02:37
I hope to know Nokia programming :( .
نوكيا | 03/11/2009, 23:53
Nice tutorial.
احدث نوكيا | كسر حماية نوكيا | تحميل كتب | برامج عمل شهادة | مشاكل نوكيا | نوكيا N97 5800 5530 | برامج نوكيا N97 5800 5530 i8910 | العاب نوكيا N97 5800 5530 i8910 | برامج نوكيا N97 5800 5530 i8910 معربه | ثيمات نوكيا N97 5800 5530 i8910 | نوكيا n73 n96 n85 8gb n95 e66 e90 e71 e63 e51 n82 n81 6290 6120 5700 N86 | برامج نوكيا n73 n96 n85 8gb n95 e66 e90 e71 e63 e51 n82 n81 6290 6120 5700 N86 | العاب نوكيا n73 n96 n85 8gb n95 e66 e90 e71 e63 e51 n82 n81 6290 6120 5700 N86 | برامج نوكيا n73 n96 n85 8gb n95 e66 e90 e71 e63 e51 n82 n81 6290 6120 5700 N86 معربه | ثيمات نوكيا n73 n96 n85 8gb n95 e66 e90 e71 e63 e51 n82 n81 6290 6120 5700 N86 | نوكيا N90 6680 3230 7610 6260 6630 6670 6600 | برامج نوكيا N90 6680 3230 7610 6260 6630 6670 6600 | العاب نوكيا N90 6680 3230 7610 6260 6630 6670 6600 | برامج نوكيا N90 6680 3230 7610 6260 6630 6670 6600 معربه | ثيمات نوكيا N90 6680 3230 7610 6260 6630 6670 6600 | العاب نوكيا مميزه | ثيمات خلفيات مميزه | برامج نوكيا مميزة | Nokia N-Gage N-Gage QD العاب انجيج برامج
Re: FileConnection - Introduction to beginners
peterblazejewicz | 19/07/2006, 19:21
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