Skip to content

Instantly share code, notes, and snippets.

@webserveis
Last active July 2, 2016 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webserveis/0f20d39b4cec25369492b0cfcebaf98b to your computer and use it in GitHub Desktop.
Save webserveis/0f20d39b4cec25369492b0cfcebaf98b to your computer and use it in GitHub Desktop.
Leer recurso directiorio res/ de Android en Java

#readFileFromRes#

Función para leer de forma binaria un recurso que este dentro de tu aplicación, directorio res/.

Su uso

Para usar la función readFileFromRes(Context context, int fileName) parámetros:

  • Context: contexto de la aplicación
  • fileName: identificador del recurso interno. R.id.recurso

ByteArrayOutputStream byteArrayOutputStream = readFileFromRes(this,R.raw.recuso);

Para obtener el conenido en formato string

byteArrayOutputStream.toString();

public ByteArrayOutputStream readFileFromRes(Context context, int fileName) {
InputStream inputStream = context.getResources().openRawResource(fileName);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int ctr;
try {
ctr = inputStream.read();
while (ctr != -1) {
outputStream.write(ctr);
ctr = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return outputStream;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment