Directory Browser ApplicationDecember 27th, 2008
Here's a sample that allows you to browse the file system on your Android. I found some sample code at http://www.linuxdevices.com/articles/AT6247038002.html and adapted it slightly.Note that it extends ListActivity and the items in the list are are rendered using the layout file file_list_row.xml. Here's the code... Here's the main layout file (main.xml)... <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/empty_dir"/> </LinearLayout> Here's the layout file for the items in the ListActivity (file_list_row.xml)... <?xml version="1.0" encoding="utf-8"?> <TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"/> Here's the main activity... package com.remwebdevelopment.android; import java.io.File; import java.util.ArrayList; import java.util.List; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class DirectoryBrowser extends ListActivity { private List<String> items = null; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); getFiles(new File("/").listFiles()); } @Override protected void onListItemClick(ListView l, View v, int position, long id){ int selectedRow = (int)id; if(selectedRow == 0){ getFiles(new File("/").listFiles()); }else{ File file = new File(items.get(selectedRow)); if(file.isDirectory()){ getFiles(file.listFiles()); }else{ new AlertDialog.Builder(FileList.this) .setTitle("This file is not a directory") .setNeutralButton("OK", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int button){ //do nothing } }) .show(); } } } private void getFiles(File[] files){ items = new ArrayList<String>(); items.add(getString(R.string.goto_root)); for(File file : files){ items.add(file.getPath()); } ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,R.layout.file_list_row, items); setListAdapter(fileList); } }
|
![]() RECENT ARTICLES
|
