SHAIKHRIYAZ : The Android Developer
Thursday, 21 January 2016
Monday, 18 January 2016
Android : Listing all files stored in the Directory and its sub directories
In this post we going to learn how to add files stored in the root directory and its sub directories to List .
Code:
Function call:
List<File> files = getListFiles(new File("root directory"));
Function:
private List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
if(file.getName().endsWith(".csv")){
inFiles.add(file);
}
}
}
return inFiles;
}
Thursday, 10 December 2015
Writing a XML file Using XmlSerializer in Android with SHAIKHRIYAZ.
Android has a special class XmlSerializer to write data in XML format . You can read everything about XmlSerializer here :
http://developer.android.com/reference/org/xmlpull/v1/XmlSerializer.html
In this post we are going to learn how to use xmlSerializer and it methods to create xml file so lets do that:
File testfile=new
File(Environment.getExternalStorageDirectory(),"rawxml.xml");
try { FileOutputStream fos=new FileOutputStream(testfile,false); XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(fos, "utf-8"); serializer.startDocument("UTF-8",null); serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output",true);
//write start tag <hello>
serializer.startTag("","hello");
// value
serializer.text("shaikhriyaz");
//write end tag </hello>
serializer.endTag("","hello");
serializer.endDocument();
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Subscribe to:
Posts (Atom)
