本文介绍了Android的 - 可配置的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个在建设我的应用程序的步骤是让用户选择,其中一些文件存储的文件夹。我怎样才能让这个配置,没有硬编码的目录。有没有我可以用它来做到这一点任何第三方库?

One of the steps in building my app is to let the user select a folder in which some files are stored. How can i make this configurable, without hardcoding the directory. Is there any 3rd party library that i can use to do this ?

推荐答案

@维克拉姆的链接提供了可使用的对话框。然后,您可以节省使用共享preferences用户选择的目录

@Vikram's link provides a dialog that you can use. You can then save the directory the user chose using Shared Preferences.

<一个href="http://stackoverflow.com/questions/3624280/how-to-use-shared$p$pferences-in-android-to-store-fetch-and-edit-values">Here关于如何使用共享preferences一个简单的例子。

Here is a simple example on how to use Shared Preferences.

可以是另一个教程中这里

更新:的开关突然打开里面我做这样的事情。积分还是去schwiz在使用这个答案为基数code。 :)

UPDATE: A switch suddenly turned on inside me to do something like this. Credits still go to schwiz in this answer for the base code used. :)

//global variables
private File[] fileList;
private String[] filenameList;

private File[] loadFileList(String directory) {
    File path = new File(directory);

    if(path.exists()) {
        FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String filename) {
                //add some filters here, for now return true to see all files
                //File file = new File(dir, filename);
                //return filename.contains(".txt") || file.isDirectory();
                return true;
            }
        };

        //if null return an empty array instead
        File[] list = path.listFiles(filter);
        return list == null ? new File[0] : list;
    } else {
        return new File[0];
    }
}

public void showFileListDialog(final String directory, final Context context) {
    Dialog dialog = null;
    AlertDialog.Builder builder = new Builder(context);

    File[] tempFileList = loadFileList(directory);

    //if directory is root, no need to up one directory
    if(directory.equals("/")) {
        fileList = new File[tempFileList.length];
        filenameList = new String[tempFileList.length];

        //iterate over tempFileList
        for(int i = 0; i < tempFileList.length; i++) {
            fileList[i] = tempFileList[i];
            filenameList[i] = tempFileList[i].getName();
        }
    } else {
        fileList = new File[tempFileList.length+1];
        filenameList = new String[tempFileList.length+1];

        //add an "up" option as first item
        fileList[0] = new File(upOneDirectory(directory));
        filenameList[0] = "..";

        //iterate over tempFileList
        for(int i = 0; i < tempFileList.length; i++) {
            fileList[i+1] = tempFileList[i];
            filenameList[i+1] = tempFileList[i].getName();
        }
    }

    builder.setTitle("Choose your file: " + directory);

    builder.setItems(filenameList, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            File chosenFile = fileList[which];

            if(chosenFile.isDirectory()) {
                showFileListDialog(chosenFile.getAbsolutePath(), context);
            }
        }
    });

    builder.setNegativeButton("Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    dialog = builder.create();
    dialog.show();
}

public String upOneDirectory(String directory) {
    String[] dirs = directory.split(File.separator);
    StringBuilder stringBuilder = new StringBuilder("");

    for(int i = 0; i < dirs.length-1; i++) {
        stringBuilder.append(dirs[i]).append(File.separator);
    }

    return stringBuilder.toString();
}

在code以上宛如一个迷你的文件浏览器,列出文件和Android的文件系统中的文件夹的行为。您可以使用它像:

The code above acts like a mini file explorer that lists the files and folders of the Android File System. You use it like:

showFileListDialog(Environment.getExternalStorageDirectory().toString(),
    MainActivity.this);

在回答你的问题:加入全球关键变量

Answering your question:Add global key variables

//package name goes here.
public static final String PACKAGE_NAME = "com.example.app";
public static final String KEY_DIRECTORY_SELECTED =
    PACKAGE_NAME + ".DIRECTORY_SELECTED";

private SharedPreferences prefs;

您使用它以前在什么地方初始化你的共享preferences上的onCreate:

initialize your SharedPreferences somewhere on onCreate before you use it:

prefs = getSharedPreferences(PACKAGE_NAME, Context.MODE_PRIVATE);

,然后你可以添加一个积极的按钮,在对话框

and then you can add a positive button to the dialog

builder.setPositiveButton("Save Directory", new OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        prefs.edit().putString(KEY_DIRECTORY_SELECTED, directory).commit();
    }
});

在你的活动中,而不是使用SD卡默认的目录即可。

In your activity, instead of using the SDCard default directory you can

//if no saved directory yet, use SDCard directory as default
String oldChosenDirectory = prefs.getString(KEY_DIRECTORY_SELECTED,
    Environment.getExternalStorageDirectory().toString());
showFileListDialog(oldChosenDirectory, MainActivity.this);

由于维克拉姆指出。你还需要做到这一点在你的FilenameFilter,这样的对话框将显示目录仅

As Vikram pointed out. you also need to do this in your FileNameFilter so that the dialog will display directories ONLY

FilenameFilter filter = new FilenameFilter() {
    public boolean accept(File dir, String filename) {
        return file.isDirectory();
    }
};

测试这一项上的仿真器。

Tested this one on an emulator.

这篇关于Android的 - 可配置的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:58