android - 浏览和上传功能在 Android 中有效吗?

标签 android cordova file-upload

<分区>

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

最佳答案

完整代码如下:

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>

关于android - 浏览和上传功能在 Android 中有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11843763/

相关文章:

jsf - 关于在 JSF 中上传文件的基本问题

java - 如何在 Android 中为字符串解析设置时区

java - "Error initializing Network Connection: Class not found",源: file:///android_asset/www/cordova.js

android - Phonegap 网络连接 - 无法读取未定义的属性 'type'

javascript - AngularJS 中的 jQuery 文件上传

jquery - 为什么我无法从文件上传进度栏获取异步行为?

android - 未可靠地调用 UtteranceProgressListener

java - Android:从mysql获取数据时doInBackground出错

Android:当焦点位于 EditText 不工作时,对话框自动显示软键盘