How to copy a file that is in the Assets folder?

1

I ask this question to know how to copy the contents of a file that is in the Assets folder but my error method because I should at least return a true value, but it does not return anything nor does it come to load My code:

    //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)){

}

If you notice where you fail or know another way. Please let me know.

    
asked by Abraham.P 06.02.2017 в 22:12
source

1 answer

1

Your method receives 3 files, it is assumed that from these you generate an InputStream, not to use it as a destination:

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

You can use the method described here , where you define the path of the file and the path in external storage (this method copies exclusively to external storage):

    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());
    }

}
    
answered by 06.02.2017 / 22:40
source