Android: almacena entradas en el archivo
Estoy recuperando un feed XML de una url y luego analizándolo. Lo que tengo que hacer también es almacenar que internamente al teléfono para que cuando no hay conexión a Internet puede analizar la opción guardada en lugar de la viva.
El problema que estoy enfrentando es que puedo crear el objeto url, use getInputStream para obtener el contenido, pero no me deja guardarlo.
- Cómo reproducir video en vivo desde URL remota en android?
- Obtener flujo de vídeo IP Cam en Android (MJEPG)
- ¿Cómo puedo ver CoverFlow?
- MediaPlayer / VideoView - Stream de una URL y luego guardar el video después de la caché?
- Transmitir vídeo desde el servidor php a android mediante programación
URL url = null; InputStream inputStreamReader = null; XmlPullParser xpp = null; url = new URL("http://*********"); inputStreamReader = getInputStream(url); ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFileAppeal.srl")); //-------------------------------------------------------- //This line is where it is erroring. //-------------------------------------------------------- out.writeObject( inputStreamReader ); //-------------------------------------------------------- out.close();
Cualquier idea de cómo puedo guardar el flujo de entrada para poder cargarlo más tarde.
Aclamaciones
- ¿Cómo transmitir vídeo directamente entre dvr (servidor) y el teléfono móvil con android (cliente)?
- Control del balance de audio en Android
- Transmisión de un archivo de audio en android a través de RTP
- ¿Cómo puedo escalar un mapa de bits en streaming sin leer primero toda la imagen?
- Lectura de un archivo binario en una cadena
- La aplicación de Android se bloquea varias veces en sucesión
- abrir y reproducir un flujo de transporte en Android (mpeg2-ts)
- Android Coverflow problema en 4.0.3 Samsung Galaxy S2
Aquí está, la entrada es su inputStreamReader
. A continuación, utilice el mismo archivo (nombre) y FileInputStream
para leer los datos en el futuro.
try { File file = new File(getCacheDir(), "cacheFileAppeal.srl"); OutputStream output = new FileOutputStream(file); try { try { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); } output.flush(); } finally { output.close(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { input.close(); }
Función Simple
Pruebe esta sencilla función para envolverla en:
// Copy an InputStream to a File. // private void copyInputStreamToFile(InputStream in, File file) { OutputStream out = null; try { out = new FileOutputStream(file); byte[] buf = new byte[1024]; int len; while((len=in.read(buf))>0){ out.write(buf,0,len); } } catch (Exception e) { e.printStackTrace(); } finally { // Ensure that the InputStreams are closed even if there's an exception. try { if ( out != null ) { out.close(); } // If you want to close the "in" InputStream yourself then remove this // from here but ensure that you close it yourself eventually. in.close(); } catch ( IOException e ) { e.printStackTrace(); } } }
Gracias a Jordan LaPrise y su respuesta .
Una versión más corta:
OutputStream out = new FileOutputStream(file); fos.write(IOUtils.read(in)); out.close(); in.close();