Dialog file select – sample


The Android framework did not think to provide a dialog to select files;I wanted to share my solution, take it as a basic structure to customize and improve it.8y3.
Example screenshot:

Java code:

public class ....... extends Activity {

        static final int DIALOG_OPENFILE_ID = 0;

        ........
        showDialog(DIALOG_OPENFILE_ID);
        ........

        @Override
        protected Dialog onCreateDialog(int id) {
                switch (id) {
                case DIALOG_OPENFILE_ID:
                        LayoutInflater li = LayoutInflater.from(this);
                        View dialogFileInputView = li.inflate(R.layout.dialog_open, null);

                        AlertDialog.Builder dialogFileInputBuilder = new AlertDialog.Builder(
                                        this);
                        dialogFileInputBuilder.setTitle("Select a file");
                        dialogFileInputBuilder.setView(dialogFileInputView);
                        AlertDialog dialogFileInput = dialogFileInputBuilder.create();

                        return dialogFileInput;
                default:
                        break;
                }
                return null;
        }

        @Override
        protected void onPrepareDialog(int id, Dialog dialog) {
                switch (id) {
                case DIALOG_OPENFILE_ID:
                        final String strRoot = "/";
                        final StringBuffer currentDir = new StringBuffer(strRoot);

                        final ArrayList<String> arrayItem = new ArrayList<String>();
                        final ArrayList<String> arrayPath = new ArrayList<String>();

                        final AlertDialog dialogFileInput = (AlertDialog) dialog;
                        final TextView textviewPathCurrent = (TextView) dialogFileInput
                                        .findViewById(R.id.dialogopenfile_tv_path);

                        textviewPathCurrent.setText(strRoot);
                        textviewPathCurrent.setTextColor(Color.RED);

                        final ListView listviewT = (ListView) dialogFileInput
                                        .findViewById(R.id.dialogopenfile_lv_itemlist);

                        refreshDir(strRoot, listviewT, arrayPath, arrayItem, this);

                        listviewT.setTextFilterEnabled(true);

                        listviewT.setOnItemClickListener(new OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> arg0, final View arg1,
                                                int position, long arg3) {

                                        final File file = new File(arrayPath.get(position));

                                        if (file.isDirectory()) {
                                                if (file.canRead()) {
                                                        textviewPathCurrent.setText(file.getPath());

                                                        currentDir.insert(0, arrayPath.get(position));
                                                        currentDir.setLength(arrayPath.get(position)
                                                                        .length());

                                                        refreshDir(arrayPath.get(position), listviewT,
                                                                        arrayPath, arrayItem, arg1.getContext());
                                                } else {
                                                        // can't access the directory
                                                        Toast.makeText(arg1.getContext(),
                                                                        "You can not get into this folder!",
                                                                        Toast.LENGTH_LONG).show();

                                                }
                                        } else {
                                                AlertDialog.Builder ConfirmDialog = new AlertDialog.Builder(
                                                                arg1.getContext());
                                                ConfirmDialog
                                                                .setMessage(
                                                                                "Are you sure you want this file ["
                                                                                                + file.getName() + "] ?")
                                                                .setCancelable(false)
                                                                .setPositiveButton("Yes",
                                                                                new DialogInterface.OnClickListener() {
                                                                                        public void onClick(
                                                                                                        DialogInterface dialog,
                                                                                                        int id) {
                                                                                                // return the selected file's
                                                                                                // path

                                                                                                Toast.makeText(
                                                                                                                arg1.getContext(),
                                                                                                                "You choose this: "
                                                                                                                                + file.getPath(),
                                                                                                                Toast.LENGTH_LONG)
                                                                                                                .show();

                                                                                                dialog.dismiss();

                                                                                                dialogFileInput.dismiss();
                                                                                        }
                                                                                })
                                                                .setNegativeButton("No",
                                                                                new DialogInterface.OnClickListener() {
                                                                                        public void onClick(
                                                                                                        DialogInterface dialog,
                                                                                                        int id) {
                                                                                                dialog.cancel();
                                                                                        }
                                                                                });
                                                AlertDialog alert = ConfirmDialog.create();
                                                alert.show();
                                        }
                                }
                        });

                        Button button = (Button) dialogFileInput
                                        .findViewById(R.id.dialogopenfile_btn_upfolder);
                        button.setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                        File file = new File(currentDir.toString());

                                        if (file.getName().equals(strRoot) == false) {

                                                refreshDir(file.getParent(), listviewT, arrayPath,
                                                                arrayItem, v.getContext());

                                                if (file.getParent().equals(strRoot) == false) {
                                                        currentDir.insert(0, file.getParent());
                                                        currentDir.setLength(file.getParent().length());
                                                }

                                                textviewPathCurrent.setText(file.getParent());
                                        }

                                }
                        });

                        break;
                default:
                        break;
                }
        }

        private void refreshDir(String strT, ListView listviewT,
                        ArrayList<String> arrayPathT, ArrayList<String> arrayItemT,
                        Context contextT) {
                arrayItemT.clear();
                arrayPathT.clear();

                // fill array adapter
                File f = new File(strT);
                File[] files = f.listFiles();

                for (int i = 0; i < files.length; i++) {
                        File file = files[i];
                        arrayPathT.add(file.getPath());
                        if (file.isDirectory())
                                arrayItemT.add(file.getName() + "/");
                        else
                                arrayItemT.add(file.getName());
                }

                // fill the list view
                listviewT.setAdapter(new ArrayAdapter<String>(contextT, R.layout.row,
                                arrayItemT));

        }
}

dialog_open.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_root"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:minWidth="300dp"
        android:minHeight="400dp"
        android:padding="10dp"
        android:orientation="vertical"
>
        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/dialogopenfile_ll_header"
        >
                <Button
                        android:layout_width="wrap_content"
                        android:text="UP"
                        android:layout_height="wrap_content"
                        android:id="@+id/dialogopenfile_btn_upfolder"
                ></Button>
                <TextView
                        android:text="TextView"
                        android:layout_width="fill_parent"
                        android:id="@+id/dialogopenfile_tv_path"
                        android:layout_height="wrap_content"
                        android:paddingLeft="5dp"
                ></TextView>
        </LinearLayout>
        <ListView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/dialogopenfile_lv_itemlist"
                android:cacheColorHint="#00000000"
        ></ListView>
</LinearLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:minHeight="?android:attr/listPreferredItemHeight" />

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...