'File Selector (.txt)
I've been trying to get a code to list all the .txt files which are stored in a directory of a mobile phone.
It's my first time with Android Studio, so it's hard for me to achieve that. To test, firstly I am trying to do it with the files that I have created in the Download folder of the emulator (in the SD), but no matter how hard I try, the application keeps closing every time I load the code.
I've tried different codes but I am not progressing, so all help is welcome.
Here, I am going to put one of the codes I've tried so far (almost everything is taken from the Internet, source http://www.aprendeandroid.com/l8/listar_archivos.htm):
public class MainActivity extends AppCompatActivity {
private ActivityResultRegistry listaFiles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tomar_datos = (Button) findViewById(R.id.Tomar_datos);
}
Button Tomar_datos;
public void onClick(View view) {
Intent i = new Intent();
i.setClass(MainActivity.this, ListaFilesActivity.class);
startActivity(i);
}
class ListaFilesActivity extends Activity {
private List<String> item = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // para quitar titulo
setContentView(R.layout.activity_main);
item = new ArrayList<String>();
File f = new File(Environment.getExternalStorageDirectory() + "/Download");
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
//Mostramos la ruta en el layout
TextView ruta = (TextView) findViewById(R.id.ruta);
ruta.setText(Environment.getExternalStorageDirectory() + "/Download");
//Localizamos y llenamos la lista
ListView lstOpciones = (ListView) findViewById(R.id.listaFiles);
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item);
lstOpciones.setAdapter(fileList);
// Accion para realizar al pulsar sobre la lista
lstOpciones.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// Devuelvo los datos a la activity principal
Intent data = new Intent();
data.putExtra("filename", item.get(position));
setResult(RESULT_OK, data);
finish();
}
});
}
}
}
Solution 1:[1]
Please just try this way of filter with list files may help you
File f = new File(Environment.getExternalStorageDirectory() + "/Download");
File[] textiles = f.listFiles(new FileFilter() {
@Override
public boolean accept(File file)
{
return (file.getPath().endsWith(".txt"));
}
});
Or
List list;//list of file or path
private void listAllTextfiles(File directory) {
final File[] files = folder.listFiles();//can add above filter for .txt file only
if ( files != null ) {
for ( File file : files ) {
if ( file != null ) {
if ( file.isDirectory() ){
listAllTextfiles(file);
}
else {
list.add(file or path)
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |