public final class FileSystemStorage
- Object
- FileSystemStorage
Unlike networking, the file system storage mostly tries to emulate java.io.File with some simplifications for mobile devices.
Check out a more thorough discussion of this API here.
A lot of API’s rely on FileSystemStorage as its the API native code usually uses consistently.
E.g. in this sample below the FileSystemStorage is used to save a screenshot image for sharing
on social media:
Form hi = new Form("ShareButton");
ShareButton sb = new ShareButton();
sb.setText("Share Screenshot");
hi.add(sb);
Image screenshot = Image.createImage(hi.getWidth(), hi.getHeight());
hi.revalidate();
hi.setVisible(true);
hi.paintComponent(screenshot.getGraphics(), true);
String imageFile = FileSystemStorage.getInstance().getAppHomePath() + "screenshot.png";
try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(imageFile)) {
ImageIO.getImageIO().save(screenshot, os, ImageIO.FORMAT_PNG, 1);
} catch(IOException err) {
Log.e(err);
}
sb.setImageToShare(imageFile, "image/png");
The sample below shows the FileSystemStorage as a tree:
Form hi = new Form("FileSystemTree", new BorderLayout());
TreeModel tm = new TreeModel() {
@Override
public Vector getChildren(Object parent) {
String[] files;
if(parent == null) {
files = FileSystemStorage.getInstance().getRoots();
return new Vector(Arrays.asList(files));
} else {
try {
files = FileSystemStorage.getInstance().listFiles((String)parent);
} catch(IOException err) {
Log.e(err);
files = new String[0];
}
}
String p = (String)parent;
Vector result = new Vector();
for(String s : files) {
result.add(p + s);
}
return result;
}
@Override
public boolean isLeaf(Object node) {
return !FileSystemStorage.getInstance().isDirectory((String)node);
}
};
Tree t = new Tree(tm) {
@Override
protected String childToDisplayLabel(Object child) {
String n = (String)child;
int pos = n.lastIndexOf("/");
if(pos
@author Shai Almog
Fields
public static final int ROOT_TYPE_MAINSTORAGE = 1 | Represents the type for the get root type method, this type generally represents the main phone memory |
public static final int ROOT_TYPE_SDCARD = 2 | Represents the type for the get root type method, this type generally represents an SD card although due to variability in phone standards an SD card might be detected incorrectly. |
public static final int ROOT_TYPE_UNKNOWN = 3 | Returned for different types of root for which there is no specific knowledge one way or the other. |
Methods
Inherited methods
Field details
ROOT_TYPE_MAINSTORAGE
public static final int ROOT_TYPE_MAINSTORAGE = 1ROOT_TYPE_SDCARD
public static final int ROOT_TYPE_SDCARD = 2ROOT_TYPE_UNKNOWN
public static final int ROOT_TYPE_UNKNOWN = 3Method details
getInstance
public static FileSystemStorage getInstance()Returns
getRoots
public String[] getRoots()Returns
getRootType
public int getRootType(String root)Parameters
rootString- the root whose type we are checking
Returns
listFiles
public String[] listFiles(String directory)
throws IOExceptionParameters
directoryString- the directory in which files should be listed
Returns
Throws
getRootSizeBytes
public long getRootSizeBytes(String root)Parameters
rootString- the root directory in the filesystem
Returns
getRootAvailableSpace
public long getRootAvailableSpace(String root)Parameters
rootString- the root directory in the filesystem
Returns
mkdir
public void mkdir(String directory)Parameters
directoryString- the directory name to create
delete
public void delete(String file)Parameters
fileString- file or empty directory to delete
deleteRetry
public void deleteRetry(String file, int retryCount)Parameters
fileString- file to delete
retryCountint- the number of times to retry
exists
public boolean exists(String file)Parameters
fileString- the file to check
Returns
isHidden
public boolean isHidden(String file)Parameters
fileString- file
Returns
setHidden
public void setHidden(String file, boolean h)Parameters
fileString- file
hboolean- hidden state
rename
public void rename(String file, String newName)Parameters
fileString- absolute file name
newNameString- relative new name
getLength
public long getLength(String file)Parameters
fileString- file
Returns
getLastModified
public long getLastModified(String file)Returns
isDirectory
public boolean isDirectory(String file)Parameters
fileString- file
Returns
getFileSystemSeparator
public char getFileSystemSeparator()Returns
openOutputStream
public OutputStream openOutputStream(String file)
throws IOExceptionParameters
fileString- the file
Returns
Throws
openInputStream
public InputStream openInputStream(String file)
throws IOExceptionParameters
fileString- the file
Returns
Throws
openOutputStream
public OutputStream openOutputStream(String file, int offset)
throws IOExceptionParameters
fileString- the file
offsetint- position in the file
Returns
Throws
getAppHomePath
public String getAppHomePath()The application home directory is a “safe place” to store files for this application in a portable way. On some platforms such as Android & iOS this path may be visible only to the application itself, other apps won’t have permission to access this path.
The sample below uses the app home directory to save a file so we can share it using the com.codename1.components.ShareButton:
Form hi = new Form("ShareButton");
ShareButton sb = new ShareButton();
sb.setText("Share Screenshot");
hi.add(sb);
Image screenshot = Image.createImage(hi.getWidth(), hi.getHeight());
hi.revalidate();
hi.setVisible(true);
hi.paintComponent(screenshot.getGraphics(), true);
String imageFile = FileSystemStorage.getInstance().getAppHomePath() + "screenshot.png";
try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(imageFile)) {
ImageIO.getImageIO().save(screenshot, os, ImageIO.FORMAT_PNG, 1);
} catch(IOException err) {
Log.e(err);
}
sb.setImageToShare(imageFile, "image/png");
Returns
hasCachesDir
public boolean hasCachesDir()Returns
getCachesDir
public String getCachesDir()#hasCachesDir()
is falseReturns
toNativePath
public String toNativePath(String path)Parameters
pathString- A file system path.