Java tutorial
//package com.java2s; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.widget.Toast; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Main { /** * Reads a file that is embedded in our application and writes it to the device storage * @param context * @param file * @param assetFileDescriptor */ private static void saveFileToDevice(Context context, File file, AssetFileDescriptor assetFileDescriptor) { // The output stream is used to write the new file to the device storage FileOutputStream outputStream = null; // The input stream is used for reading the file that is embedded in our application FileInputStream inputStream = null; try { byte[] buffer = new byte[1024]; // Create the input stream inputStream = (assetFileDescriptor != null) ? assetFileDescriptor.createInputStream() : null; // Create the output stream outputStream = new FileOutputStream(file, false); // Read the file into buffer int i = (inputStream != null) ? inputStream.read(buffer) : 0; // Continue writing and reading the file until we reach the end while (i != -1) { outputStream.write(buffer, 0, i); i = (inputStream != null) ? inputStream.read(buffer) : 0; } outputStream.flush(); } catch (IOException io) { // Display a message to the user Toast toast = Toast.makeText(context, "Could not save the file", Toast.LENGTH_SHORT); toast.show(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException ex) { // We should really never get this far, but we might consider adding a // warning/log entry here... } } if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { // We should really never get this far, but we might consider adding a // warning/log entry here... } } } } }