1

Formulo esta pregunta para saber como copiar el contenido de un fichero que se encuentra en la carpeta Assets pero mi método de da error porque me debería al menos retornar un valor verdadero, pero no retorna nada ni se llega a cargar Mi codigo:

    //Este es el método que hago mención desde un boton y el botón funciona  
    private boolean Copiar55(File a , File b, File c) throws IOException{
    final int CHUNK_SIZE = 1024 * 4;

    InputStream is=getAssets().open("texto1.txt");
    OutputStream os = new BufferedOutputStream(new FileOutputStream(a));
    byte[] chunk = new byte[CHUNK_SIZE];
    int bytesLeidos = 0;

    while ( (bytesLeidos = is.read(chunk)) > 0) {
        os.write(chunk, 0, bytesLeidos);
    }
    os.close();
    String si="si";

    InputStream is1=getAssets().open("texto2.txt");
    OutputStream os1 = new BufferedOutputStream(new FileOutputStream(b));
    byte[] chunk1 = new byte[CHUNK_SIZE];
    int bytesLeidos1 = 0;

    while ( (bytesLeidos1 = is1.read(chunk1)) > 0) {
        os.write(chunk1, 0, bytesLeidos1);
    }
    os1.close();

    InputStream is2=getAssets().open("texto3.txt");
    OutputStream os2 = new BufferedOutputStream(new FileOutputStream(b));
    byte[] chunk2 = new byte[CHUNK_SIZE];
    int bytesLeidos2 = 0;

    while ( (bytesLeidos2 = is2.read(chunk1)) > 0) {
        os.write(chunk2, 0, bytesLeidos2);
    }
    os2.close();


    return true;

}

//Esto es lo que utilizo para llamar a mi método desde el boton
if(Copiar55(a,b,c)){

}

Si notan donde fallo o saben otra forma. Por favor comunicármelo.

5
  • ¿Podrías compartir el error? (StackTrace, depuración) el 6 feb. 2017 a las 21:28
  • Deberías agregarle un contexto a tu método y anteponerlo como esto.. context.getAssets().open("texto1.txt");
    – marlonpya
    el 6 feb. 2017 a las 21:28
  • Si declaró el botón dentro de una Activity, tiene un contexto. el 6 feb. 2017 a las 21:32
  • Ok lo intentare muchas gracias.
    – Abraham.P
    el 6 feb. 2017 a las 21:35
  • Debe retornar un valor boleano pero no retorna nada?
    – Jorgesys
    el 6 feb. 2017 a las 21:36

1 respuesta 1

1

Tu método recibe 3 archivos se supone que a partir de estos generas un InputStream, no para usarlo como destino:

  OutputStream os = new BufferedOutputStream(new FileOutputStream(a));

Puedes usar el método descrito aquí , donde defines el path del archivo y el path en el almacenamiento externo (este método copia exclusivamente al almacenamiento externo):

    private void copyAssets(String path, String outPath) {
    AssetManager assetManager = this.getAssets();
    String assets[];
    try {
        assets = assetManager.list(path);
        if (assets.length == 0) {
            copyFile(path, outPath);
        } else {
            String fullPath = outPath + "/" + path;
            File dir = new File(fullPath);
            if (!dir.exists())
                if (!dir.mkdir()) Log.e(TAG, "No create external directory: " + dir );
            for (String asset : assets) {
                copyAssets(path + "/" + asset, outPath);
            }
        }
    } catch (IOException ex) {
        Log.e(TAG, "I/O Exception", ex);
    }
}

private void copyFile(String filename, String outPath) {
    AssetManager assetManager = this.getAssets();

    InputStream in;
    OutputStream out;
    try {
        in = assetManager.open(filename);
        String newFileName = outPath + "/" + filename;
        out = new FileOutputStream(newFileName);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        out.flush();
        out.close();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }

}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.