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
Regular Contributor
bb_develop
Posts: 61
Registered: ‎02-24-2010
My Device: Storm 2

Help in File Connection

Hi..

 

I am trying to access files through file connection api. i am giving the path

 file:///SDCard/

and 

file:///store/

 

But its not picking the path.. I want to know where is the root directory file:/// located on my computer so that i can create the "SDCard" and "store" folder in that.

 

Please help.. I am pasting the code below if someone needs to read the code.

 

 

 

 

class FileConnectionScreen extends MainScreen
{
private ObjectListField fileList;
private String currentPath = "file:///";
FileConnectionScreen()
{
setTitle("FileConnection");
fileList = new ObjectListField();
fileList.set(new String[] {"store/", "SDCard/"});
add(fileList);
}
protected void makeMenu(Menu menu, int instance) 
{
super.makeMenu(menu, instance);
menu.add(new MenuItem("Select", 10, 10) 
{
public void run() 
{
loadFile();
}
});
}
private void loadFile() {
currentPath += fileList.get(fileList, fileList.getSelectedIndex());
try 
{
FileConnection fileConnection = (FileConnection)Connector.open(currentPath);
if (fileConnection.isDirectory()) {
Enumeration directoryEnumerator = fileConnection.list();
Vector contentVector = new Vector();
while(directoryEnumerator.hasMoreElements()) {
contentVector.addElement(directoryEnumerator.nextElement());
}
String[] directoryContents = new String[contentVector.size()];
contentVector.copyInto(directoryContents);
fileList.set(directoryContents);
}
} catch (IOException ex) {
}
}
}
class FileConnectionScreen extends MainScreen
{
	private ObjectListField fileList;
	private String currentPath = "file:///";
	FileConnectionScreen()
	{
		setTitle("FileConnection");
		fileList = new ObjectListField();
		fileList.set(new String[] {"store/", "SDCard/"});
		add(fileList);
	}
	protected void makeMenu(Menu menu, int instance) 
	{
		super.makeMenu(menu, instance);
		menu.add(new MenuItem("Select", 10, 10) 
		{
			public void run() 
			{
				loadFile();
			}
		});
	}

private void loadFile() {
	currentPath += fileList.get(fileList, fileList.getSelectedIndex());
	try 
	{
		FileConnection fileConnection = (FileConnection)Connector.open(currentPath);
		if (fileConnection.isDirectory()) {
			Enumeration directoryEnumerator = fileConnection.list();
			Vector contentVector = new Vector();
			while(directoryEnumerator.hasMoreElements()) {
				contentVector.addElement(directoryEnumerator.nextElement());
			}
			String[] directoryContents = new String[contentVector.size()];
			contentVector.copyInto(directoryContents);
			fileList.set(directoryContents);
		}
	} catch (IOException ex) {
	}
}
}

 

 

Please use plain text.
Developer
dpreussler
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: Help in File Connection

1st: please use FileSystemRegistry.listRoots instead of hardcoding SDCard or store. This could break someday.

 

What happens exactly? Did you check for an IOException? Because your are not logging the exception.

On which device are you testing?

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Please use plain text.
Developer
Eugen
Posts: 467
Registered: ‎07-16-2009
My Device: Z10 LE

Re: Help in File Connection

As I understand you are talking about simulator and ability to specify PC folder path as SDcard. Please open simulator options, find tab memory, scroll down to option "Use PC filesystem for SD card files".

 

About the code - don't forget to close connection in finally block.

 

Good luck!

Please use plain text.
Regular Contributor
bb_develop
Posts: 61
Registered: ‎02-24-2010
My Device: Storm 2

Re: Help in File Connection

Hi..

thanx..

I checked the option of "Use PC filesystem for SD card files" in the memory card tab of simulator( I am using Eclipse) and formed the SDCard folder on desktop and its working for File connection api.. Now I am trying the same with SQLite. I am giving the same setting and path for the SD card but its not working. On debugging i found that FileSystem Registry is not finding the root as /SDcard. Please help me out..

 

I am giving the code i have written below..

All the directories specified in the path are present in the SDCard folder on the desktop.

 

 

import java.util.Enumeration;

import javax.microedition.io.file.FileSystemRegistry;

import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseException;
import net.rim.device.api.database.DatabaseFactory;
import net.rim.device.api.database.DatabaseSecurityOptions;
import net.rim.device.api.io.MalformedURIException;
import net.rim.device.api.io.URI;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;


public class CreateDatabase extends UiApplication
{
	public static void main(String[] args)
	{
		CreateDatabase theApp = new CreateDatabase();
		theApp.enterEventDispatcher();
	}
	public CreateDatabase()
	{
		pushScreen(new CreateDatabaseScreen());
	}
}
class CreateDatabaseScreen extends MainScreen
{
	private static String DB_NAME = "Mydatabase"; 
	String dbLocation = "SDCard/BlackBerry/Databases/DatabaseDemo/";
	boolean sdCardPresent = false;
    String root = null;
	public CreateDatabaseScreen()
	{
		LabelField title = new LabelField("SQLite Create Database Sample",
				LabelField.ELLIPSIS |
				LabelField.USE_ALL_WIDTH);
		setTitle(title);
		add(new RichTextField("Creating a database called " + "MyTestDatabase on the SDCard."));
		Enumeration e = FileSystemRegistry.listRoots();
        while (e.hasMoreElements())
        {
            root = (String)e.nextElement();
            if(root.equalsIgnoreCase("sdcard/"))
            {
                sdCardPresent = true;
            }     
        }            
        if(!sdCardPresent)
        {
              dbLocation = "/store/home/user/";                                
        }          
        
        
		try
		{
			URI uri = URI.create(dbLocation + DB_NAME); 
			Database db = DatabaseFactory.openOrCreate(uri, new DatabaseSecurityOptions(false));
			db.close();
		}
		catch ( MalformedURIException e1 )
		{
			System.out.println( e1.getMessage() );
			e1.printStackTrace();
		}
		catch(DatabaseException e2)
		{
			
		}
	}
}

 

 

 

 

Please use plain text.