【问题标题】:Is browse and upload function work in Android? [duplicate]浏览和上传功能在 Android 中有效吗? [复制]
【发布时间】:2012-08-04 08:06:39
【问题描述】:

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

【问题讨论】:

  • 由于我之前没有使用过PhoneGap,所以希望这个Java Android project能给你加分……
  • @SheIs_LeThiCongNhan 谢谢我会通过你指定的链接。
  • 不客气 :-) 不幸的是,我无法在 PhoneGap 上提供帮助。关于你的目标,据我所知,你不需要任何许可。
  • 我找到了 PhoneGap here 的文件选择器。

标签: android cordova file-upload


【解决方案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();
    }
}
}

ma​​in.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>

【讨论】:

  • 您好,感谢您的快速响应,我将查看您附加的代码。但是我正在研究 phonegap,所以你有任何示例如何使用 phonegap 浏览文件吗?
  • 对不起兄弟..我还没有在 phonegap 上工作过!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 2011-06-10
相关资源
最近更新 更多