这是我准备的最终代码(来自谷歌的有价值的输入):
public class Fexplorer extends ListActivity {
String B=null ;
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.explorer_main);
myPath = (TextView)findViewById(R.id.path);
Button OK = (Button)findViewById(R.id.button2);
Button Cancel = (Button)findViewById(R.id.button1);
OK.setOnClickListener(onOK);
Cancel.setOnClickListener(onCancel);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Choose Location");
B = dirPath;
Toast.makeText(getApplicationContext(),B, Toast.LENGTH_LONG).show();
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}
/*else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();
} */
}
private View.OnClickListener onOK=new View.OnClickListener() {
public void onClick(View v){
copy();
Intent About = new Intent(Fexplorer.this,EduBridge.class);
startActivity(About);
}
};
private View.OnClickListener onCancel=new View.OnClickListener() {
public void onClick(View v){
Intent About = new Intent(Fexplorer.this,EduBridge.class);
startActivity(About);
}
};
public void copy(){
try {
File sourceFile = new File("/sdcard/edu/Brochure.pdf");
File destinationFile = new File(B+"/" + sourceFile.getName());
String X = (B+"/" + sourceFile.getName());
Toast.makeText(getApplicationContext(),X, Toast.LENGTH_LONG).show();
Fexplorer copyFileExample = new Fexplorer();
copyFileExample .copyFile(sourceFile, destinationFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public void copyFile(File sourceFile, File destinationFile) {
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
FileOutputStream fileOutputStream = new FileOutputStream(
destinationFile);
int bufferSize;
byte[] bufffer = new byte[512];
while ((bufferSize = fileInputStream.read(bufffer)) > 0) {
fileOutputStream.write(bufffer, 0, bufferSize);
}
fileInputStream.close();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
在样式中:
<style name="MyFloatingWindow" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
explorer_main XML 文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity ="center">
<TextView
android:id="@+id/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_below="@+id/path"
android:layout_height="308dp" />
<TextView
android:id="@android:id/empty"
android:layout_below="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No Data"
/>
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@android:id/empty"
android:text="@string/DirB" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentLeft="true"
android:text="@string/DirA" />
最后是 row.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowtext"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:textSize="15dp" />