leer un archivo específico desde una tarjeta SD en Android


Cómo leer un archivo específico desde una tarjeta SD. he empujado el archivo en la tarjeta SD a través de DDMS y estoy tratando de leerlo de esta manera, pero esto me da una excepción. ¿alguien puede decirme cómo apuntar exactamente en ese archivo?

Mi código es este.

String path = Environment.getExternalStorageDirectory().getAbsolutePath();
FileInputStream iStream =  new FileInputStream(path);
Author: Cristian, 2010-09-23

2 answers

Está intentando leer un directorio... ¡lo que necesitas es el archivo! Haz algo como esto... luego, puede leer el archivo como desee.

File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
 47
Author: Cristian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-23 15:35:21

Para leer cualquier archivo(CSV en mi caso) del Almacenamiento externo, necesitamos una ruta para él,una vez que tenga ruta puede hacer así...

void readFileData(String path) throws FileNotFoundException 
    {

        String[] data;
        File file = new File(path);
        if (file.exists())
        {
            BufferedReader br = new BufferedReader(new FileReader(file));
            try
            {
                String csvLine;
                while ((csvLine = br.readLine()) != null)
                {
                    data=csvLine.split(",");
                    try
                    {
                        Toast.makeText(getApplicationContext(),data[0]+" "+data[1],Toast.LENGTH_SHORT).show();
                    }
                    catch (Exception e)
                    {
                        Log.e("Problem",e.toString());
                    }
                }
            }
            catch (IOException ex)
            {
                throw new RuntimeException("Error in reading CSV file: "+ex);
            }
        }
        else
        {
            Toast.makeText(getApplicationContext(),"file not exists",Toast.LENGTH_SHORT).show();
        }
    }

/*
csv file data

17IT1,GOOGLE
17IT2,AMAZON
17IT3,FACEBOOK*/
 0
Author: JAYESH,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-16 04:11:35