Ruta de Android al archivo txt de activos


Estoy haciendo:

FileReader fin = new FileReader("file:///android_asset/myFile.txt");

En un proyecto Android y muchas variaciones. En tiempo de ejecución obtengo una excepción de archivo no encontrado. El archivo está presente y es correcto en la carpeta assets, por lo que mi ruta debe ser incorrecta.

¿Cuál es el camino absoluto que necesito aquí?

Author: SK9, 2011-01-25

6 answers

AssetFileDescriptor descriptor = getAssets().openFd("myfile.txt");
FileReader reader = new FileReader(descriptor.getFileDescriptor());

Intente usar lo anterior con los descriptores de archivos. Parece ser la forma más infalible que he encontrado para reunir rutas de activos.

 42
Author: kcoppock,
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
2011-01-25 13:32:26
    InputStream is = getResources().getAssets().open("terms.txt");
    String textfile = convertStreamToString(is);

public static String convertStreamToString(InputStream is)
            throws IOException {
            Writer writer = new StringWriter();

            char[] buffer = new char[2048];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,
                        "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            String text = writer.toString();
            return text;
    }
 19
Author: jayesh kavathiya,
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
2012-03-03 07:01:38

Puedes usar algo como

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(context.getAssets().open("fileName.txt")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 3
Author: Nick Campion,
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
2011-01-25 02:13:15

No lo está leyendo porque todos los activos en la carpeta assets están comprimidos, intente cambiar su extensión a .mp3 y luego léalo, eso debería evitar que se comprima.

 3
Author: panthro,
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
2011-11-01 16:11:24

He encontrado que si está utilizando un IDE como Eclipse puede que necesite hacer una limpieza clean o eliminar la API en el directorio bin. Parece que los activos no se actualiza cuando se hace una compilación.

 2
Author: Delicious Software,
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
2011-10-27 12:06:43
AssetManager am = context.getAssets();
InputStream fs = am.open("myFile.txt");
 1
Author: user598417,
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
2011-02-01 13:29:33