How to save files to a sub-folder?

229 views
Skip to first unread message

Pai Peng

unread,
Jul 31, 2013, 6:19:51 AM7/31/13
to codenameone...@googlegroups.com
Hello CN1,


my app will create some images(from camera or album), which are stored locally under installation path. Read/Write is no problem until storage to a sub-folder.

Because there could be many images, I would like to storage them under a sub-folder.

I have tried to make a directory using 
FileSystemStorage.getInstance().mkdir("photos");

but I can't find it on Simulator. (/Users/username/.cn1)

I have also tried to storage the photo directly with sub-folder path, like "photos/image.png", but i get error:
OutputStream save = Storage.getInstance().createOutputStream(FILE_PHOTO_DIR + "/" + fileName);



java.io.IOException: Illegal charcter '/' in storage name: photos/image.png

The first question is how to make a folder under the installation path?
and the second: how to storage files in the sub-folder?

Thanks

Pai



Chen Fishbein

unread,
Jul 31, 2013, 9:56:22 AM7/31/13
to codenameone...@googlegroups.com
Hi,
You need to create dirs under your home directory use:

String home = FileSystemStorage.getInstance().getAppHomePath();

and put your dir and files under the home path.

char sep = FileSystemStorage.getInstance().getFileSystemSeparator();
FileSystemStorage.getInstance().mkdir(home + sep + "photos");

Storage class is used to save small data objects not files.

Pai Peng

unread,
Jul 31, 2013, 3:33:10 PM7/31/13
to codenameone...@googlegroups.com
Helo Chen,

Thank you for replaying me.

I have also tried the AppHomePath() but the result is not correct.

So, I am working on a Macbook OS X 10.7.5 with Java 7. By testing with simulator, the result of getAppHomePath():
/Users/username

But the app is installed under
/Users/username/.cn1

Could it be a bug?


To the second question, I use Storage to save Image object to files. And now I want to save them under a sub-folder, like "/Users/username/.cn1/photos". But I still have problem by sub-folder.


Regards,

Pai

Shai Almog

unread,
Aug 1, 2013, 12:43:39 AM8/1/13
to codenameone...@googlegroups.com
Hi,
home path is the system home not the app installation dir which is OS specific. You in the simulator we return the home dir of the OS which is what you got.
You can write to that directory safely and create folders within it.

Storage doesn't support folders since it isn't really a filesystem, its designed for ultimate portability. If you want hierarchies you need to use the FileSystemStorage.
Reply all
Reply to author
Forward
0 new messages