Develop on Android
Android Lab Test
www.AndroidLabTest.com
Youku
By Bruno Delb
www.weibo.com/brunodelb
i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn
http://i.youku.com/brunoparis
Weibo
Officialsite
Lesson : The list of root directories
The list of the root directories
• In this lesson, you will learn to use the file manager to list
the list of root files.
• For this, you will use File and listFiles.
The list of the root directories
• In order to know the path to the application directory, use the
method getFilesDir().
File file = this.getFilesDir();
• In order to know the absolute path to a directory, use the method
getAbsolutePath() :
String text = "Local storage : " + file.getAbsolutePath();
The list of the root directories
• To get the list of directories and files at the root, use the
method listRoots().
File[] files = File.listRoots();
The list of the root directories
• To know if an entry of a directory is a directory, use the method
isDirectory() :
if (file.isDirectory()) {
}
• To get he list of the files and directories of a directory, use the
method listFiles() :
rootFile.listFiles()
Layout main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/et_output"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10" >
</EditText>
</LinearLayout>
File Main.java
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et_output = (EditText)findViewById (R.id.et_output);
File file = this.getFilesDir();
String text = "Local storage : " + file.getAbsolutePath();
File[] files = File.listRoots();
for (int i = 0; i < files.length; i++){
String filename = files[i].getAbsolutePath();
filename.replaceAll (new String(new byte[]{0}), "");
File rootFile = new File (filename);
if (file.isDirectory()) {
for (int j = 0; j < rootFile.listFiles().length; j++){
text += rootFile.listFiles() [j].getAbsolutePath() + "rn";
}
}
}
et_output.setText (text);
}
}
Test on your mobile
File_ListRoots
Follow me on my channel PengYooTV …
On my Youku channel
http://i.youku.com/brunoparis
Who am I ?
Bruno Delb (www.delb.cn),
Author of the first french book of development of Java mobile application (2002),
Consultant, project manager and developer of social & mobile applications,
let’s talk about your needs ...
And on Weibo :
http://www.weibo.com/brunodelb

Android Lab Test : Reading the foot file list (english)

  • 1.
    Develop on Android AndroidLab Test www.AndroidLabTest.com Youku By Bruno Delb www.weibo.com/brunodelb i.youku.com/brunodelb | www.weibo.com/brunodelb | blog.delb.cn http://i.youku.com/brunoparis Weibo Officialsite Lesson : The list of root directories
  • 2.
    The list ofthe root directories • In this lesson, you will learn to use the file manager to list the list of root files. • For this, you will use File and listFiles.
  • 3.
    The list ofthe root directories • In order to know the path to the application directory, use the method getFilesDir(). File file = this.getFilesDir(); • In order to know the absolute path to a directory, use the method getAbsolutePath() : String text = "Local storage : " + file.getAbsolutePath();
  • 4.
    The list ofthe root directories • To get the list of directories and files at the root, use the method listRoots(). File[] files = File.listRoots();
  • 5.
    The list ofthe root directories • To know if an entry of a directory is a directory, use the method isDirectory() : if (file.isDirectory()) { } • To get he list of the files and directories of a directory, use the method listFiles() : rootFile.listFiles()
  • 6.
    Layout main.xml <?xml version="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:id="@+id/et_output" android:layout_width="match_parent" android:layout_height="match_parent" android:ems="10" > </EditText> </LinearLayout>
  • 7.
    File Main.java public classMain extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); EditText et_output = (EditText)findViewById (R.id.et_output); File file = this.getFilesDir(); String text = "Local storage : " + file.getAbsolutePath(); File[] files = File.listRoots(); for (int i = 0; i < files.length; i++){ String filename = files[i].getAbsolutePath(); filename.replaceAll (new String(new byte[]{0}), ""); File rootFile = new File (filename); if (file.isDirectory()) { for (int j = 0; j < rootFile.listFiles().length; j++){ text += rootFile.listFiles() [j].getAbsolutePath() + "rn"; } } } et_output.setText (text); } }
  • 8.
    Test on yourmobile File_ListRoots
  • 9.
    Follow me onmy channel PengYooTV … On my Youku channel http://i.youku.com/brunoparis Who am I ? Bruno Delb (www.delb.cn), Author of the first french book of development of Java mobile application (2002), Consultant, project manager and developer of social & mobile applications, let’s talk about your needs ... And on Weibo : http://www.weibo.com/brunodelb