Java tutorial
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.net.Uri; public class Main { /** * Copy a file. * @param src Source file. * @param dst Destination file. * @return {@code true} if the copy succeeded, {@code false} otherwise. */ public static boolean copyFile(File src, File dst) { boolean retVal = false; try { InputStream inStream = new FileInputStream(src); try { OutputStream outStream = new FileOutputStream(dst); try { copyFile(inStream, outStream); retVal = true; } finally { outStream.close(); } } finally { inStream.close(); } } catch (IOException e) { e.printStackTrace(); } return retVal; } /** * Copy a file from an Uri. * @param ctx Context. * @param src Source file. * @param dst Destination file. * @return {@code true} if the copy succeeded, {@code false} otherwise. */ public static boolean copyFile(Context ctx, Uri srcUri, File dst) { boolean retVal = false; try { InputStream inStream = ctx.getContentResolver().openInputStream(srcUri); try { OutputStream outStream = new FileOutputStream(dst); try { copyFile(inStream, outStream); retVal = true; } finally { outStream.close(); } } finally { inStream.close(); } } catch (IOException e) { e.printStackTrace(); } return retVal; } protected static void copyFile(InputStream inStream, OutputStream outStream) throws IOException { byte[] buf = new byte[2048]; int len; while ((len = inStream.read(buf)) > 0) { outStream.write(buf, 0, len); } } }