list files / files on android

2

I would like to be able to open the files in a ListView of an activity  that contains my application. These files are being saved in another activity, and always with the same extension ( .prop for making it unique).

What I do not know is how to get the list of these files in an array (since I have no idea where each file is stored .prop ).

I attached the code when I save each file:

final EditText input = new EditText(Insercion.this);

new AlertDialog.Builder(Insercion.this)
        .setTitle("Nombre del fichero")
        .setView(input)
        .setPositiveButton("Guardar", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String editable = input.getText().toString();
                // aquí guardo lo aceptado
                try {
                FileOutputStream fos = openFileOutput(editable+".prop", Context.MODE_WORLD_READABLE);

                ObjectOutputStream salida=new ObjectOutputStream(fos);
                salida.writeObject(carteles);
                fos.close();
                salida.close();
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("ERROR ESCRITURA");
            }
            }
        })
        .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

            }
        }).show();
    
asked by Sergio Cv 15.04.2016 в 11:28
source

1 answer

2

First you must take into account that to write a file, you must have a place to write it, therefore this is incorrect:

FileOutputStream fos = openFileOutput(editable+".prop", Context.MODE_WORLD_READABLE);

This would be the right thing to do if you wanted to write a file in the storage external :

 try {
            //Escribiendo al almacenamiento externo.
            FileOutputStream fos = new FileOutputStream(new File(getExternalFilesDir(null), "test.prop"));
            ObjectOutputStream salida = new ObjectOutputStream(fos);
            salida.writeObject(carteles);
            fos.close();
            salida.close();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("ERROR ESCRITURA");
        }

Do not forget to add permission to perform this operation within your AndroidManifest.xml :

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

If you would like to write your file within internal storage , it would be like this:

FileOutputStream fos = new FileOutputStream(new File(getFilesDir(), "test.prop"));

To obtain the files within a directory is knowing its location, which would be obtained in this way, according to your code:

  String path = getExternalFilesDir(null)+ File.separator;

The path would be:

/storage/emulated/0/Android/data/[PAQUETE DE APLICACIÓN]/files/

This would be a method to get the files inside the directory, and add them to a listView.

   private void listFilesProp(){
        List<String> list = new ArrayList<String>();
        //obtiene ruta donde se encuentran los archivos.
        String path = getExternalFilesDir(null)+ File.separator;
        File f = new File(path);
        //obtiene nombres de archivos dentro del directorio. 
        File file[] = f.listFiles();
        for (int i=0; i < file.length; i++)
        {
            Log.d("Files", "Archivo : " + file[i].getName());
            //Agrega nombres de archivos a List para ser agregado a adapter.
            list.add(file[i].getName());
        }

        ListView listview = (ListView)findViewById(R.id.listview);
        //Crea Adapter
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                list );
        //Configura Adapter a ListView.
        listview.setAdapter(arrayAdapter);
    }

    
answered by 19.04.2016 в 06:20