0

可能重复:
从 sdcard 读取 .xml 文件

我正在开发一个 Android 应用程序,并希望允许用户从本地设备上传 XML 文件。我想让用户浏览从 /sdcard 开始的设备文件。你能帮我这样做吗?

提前致谢 !

4

2 回答 2

0

我猜你正在寻找文件选择器。这是android
中一个简单的文件选择器的教程。

于 2012-12-21T10:07:30.003 回答
0
    private File path = new File(Environment.getExternalStorageDirectory() + "");

    private void loadFileList() {
            try {

                path.mkdirs();
            } catch (SecurityException e) {
                Log.e(TAG, "unable to write on the sd card ");
            }
            if (path.exists()) {
                FilenameFilter filter = new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String fn) {
                        File sel = new File(dir, fn);
                        if (sel.isDirectory() && !sel.isHidden()) {
                            int jjj = sel.list().length;
                            if (jjj >= 1)
                                return true;
                            else
                                return false;
                        } else if (sel.isFile() && !sel.isHidden()) {
                            boolean b = fn.endsWith(".xml");
                            return b;

                        } else {
                            return false;
                        }

                    }
                };

                String[] fList = path.list(filter);
                fileList = new Item[fList.length];
                for (int i = 0; i < fList.length; i++) {

                    fileList[i] = new Item(fList[i], R.drawable.file_icon);

                    File sel = new File(path, fList[i]);

                    if (sel.isDirectory()) {
                        fileList[i].icon = R.drawable.directory_icon;
                        Log.d("DIRECTORY", fileList[i].file);
                    } else {
                        Log.d("FILE", fileList[i].file);

                    }
                }

                if (!firstLvl) {
                    Item temp[] = new Item[fileList.length + 1];
                    for (int i = 0; i < fileList.length; i++) {
                        temp[i + 1] = fileList[i];
                    }
                    temp[0] = new Item("Up", R.drawable.directory_up);
                    fileList = temp;
                }
            } else {
                Log.e(TAG, "path does not exist");
            }

            adapter = new ArrayAdapter<Item>(this,
                    android.R.layout.select_dialog_item, android.R.id.text1,
                    fileList) {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {

                    // creates view
                    View view = super.getView(position, convertView, parent);

                    TextView textView = (TextView) view
                            .findViewById(android.R.id.text1);

                    // put the image on the text view

                    textView.setCompoundDrawablesWithIntrinsicBounds(
                            fileList[position].icon, 0, 0, 0);

                    // add margin between image and text (support various screen
                    // densities)
                    int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
                    textView.setCompoundDrawablePadding(dp5);

                    return view;
                }
            };

        }

// Don't Forget To Add Permission for SD Card 
于 2012-12-21T09:52:06.853 回答