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í?
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.
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;
}
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();
}
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.
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.
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");
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