3

我有一个应用程序,其中有一个按钮浏览,onclick 应该打开我的 android 手机中存在的所有文件和文件夹。我怀疑这是否可以浏览和选择要上传的文件?我观察到<input type="file">由于安全原因,它在android中不起作用。谁能告诉我一些关于如何从移动设备浏览文件以上传的好的工作示例?

4

1 回答 1

1

这是完整的代码::

AndroidExplorer.class

public class AndroidExplorer extends ListActivity {

private List<String> item = null;
private List<String> path = null;
private String root="/";
private TextView myPath;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myPath = (TextView)findViewById(R.id.path);
    getDir(root);
}

private void getDir(String dirPath)
{
    myPath.setText("Location: " + dirPath);

    item = new ArrayList<String>();
    path = new ArrayList<String>();

    File f = new File(dirPath);
    File[] files = f.listFiles();

    if(!dirPath.equals("/"))
    {

        item.add(root);
        path.add(root);

        item.add("../");
        path.add(f.getParent());

    }

    for(int i=0; i < files.length; i++)
    {
            File file = files[i];
            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) {

    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.icon)
            .setTitle("[" + file.getName() + "] folder can't be read!")
            .setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                        }
                    }).show();
        }
    }
    else
    {
        new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle("[" + file.getName() + "]")
            .setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                        }
                    }).show();
    }
}
}

主要的.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"
>
<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_height="wrap_content" 
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="No Data"
/>
</LinearLayout>
于 2012-08-07T10:32:53.797 回答