选择在android中保存文件的位置

时间:2013-12-20 17:52:51

标签: java android

我有一个对象(“jsonized”到一个字符串),我想将它保存在一个文件中。 此外,我希望显示一个对话框,允许用户选择要保存文件的文件夹。 (当我突然解决这个问题时,我正在写这个问题。只是分享我的解决方案以获得更好的方法来做到这一点的反馈)

1 个答案:

答案 0 :(得分:1)

单击“保存文件”按钮时,会启动“selectFolder()”函数

Activity.java

    public void selectFolder(){

    // Instantiate an AlertDialog.Builder with its constructor
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    // Chain together various setter methods to set the dialog characteristics
    builder.setTitle("Choose folder to save profile");

    // Get the layout inflater
    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog_selectfolder, null);

    ListView lvDirectories = (ListView) dialogView.findViewById(R.id.lvDirectories);
    String path = Environment.getExternalStorageDirectory().toString();
    ((TextView) dialogView.findViewById(R.id.tvJamesBond)).setText(path);

    final ArrayList<String> items = listFolders(path);
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);


    lvDirectories.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String dest = ((ListView) dialogView.findViewById(R.id.lvDirectories)).getItemAtPosition(i).toString().trim();
            String path;
            if(dest.compareTo("...")==0){
                int lastSlash = ((TextView) dialogView.findViewById(R.id.tvJamesBond)).getText().toString().lastIndexOf("/");
                path =  ((TextView) dialogView.findViewById(R.id.tvJamesBond)).getText().toString().substring(0,lastSlash);
            }
            else{
                path = ((TextView) dialogView.findViewById(R.id.tvJamesBond)).getText().toString() + "/" + dest;
            }
            items.clear();
            items.addAll(listFolders(path));
            ((TextView) dialogView.findViewById(R.id.tvJamesBond)).setText(path);
            adapter.notifyDataSetChanged();
        }
    });

    lvDirectories.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    builder.setView(dialogView);

    // Add the buttons
    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            saveProfile(((TextView) dialogView.findViewById(R.id.tvJamesBond)).getText().toString());
        }
    });

    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Toast toast = Toast.makeText(getApplicationContext(), "Nothing saved", Toast.LENGTH_SHORT);
            toast.show();
        }
    });

    // Get the AlertDialog from create()
    AlertDialog dialog = builder.create();
    dialog.show();

}

public ArrayList<String> listFolders(String path){
    ArrayList<String> result = new ArrayList<String>();
    File f = new File(path);
    File[] files = f.listFiles();
    Log.d("TEST PATH1", path);
    Log.d("TEST PATH1", Environment.getExternalStorageDirectory().toString());
    if(path.compareTo(Environment.getExternalStorageDirectory().toString())!=0){
        result.add("...");
    }
    for (File inFile : files) {
        if (inFile.isDirectory()) {
            result.add(inFile.getName());
        }
    }

    return result;
}

public void saveProfile(String folder){

    String fileName = "default.txt";

    try{
        String ob = new Gson().toJson(((MyApplication)getApplication()).getProfile());
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(folder+"/"+fileName));
        outputStreamWriter.write(ob);
        outputStreamWriter.close();
    }
    catch(IOException ex){
        Log.e("SAVE_FILE", ex.toString());
    }

    Toast toast = Toast.makeText(getApplicationContext(), "Profile saved to file '"+fileName+"'", Toast.LENGTH_SHORT);
    toast.show();

}

dialog_selectfolder.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lvDirectories"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="invisible"
        android:text="New Text"
        android:id="@+id/tvJamesBond"
        android:layout_gravity="center_horizontal" />
</LinearLayout>