Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.File;

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 uri file to indicated file path
     * 
     * @param context   the application context
     * @param uri       the uri file to copy
     * @param dest      the dest file 
     * @throws IOException
     */
    public static void copyUriTo(Context context, Uri uri, File dest) throws IOException {
        InputStream input = null;
        OutputStream output = null;

        try {
            input = context.getContentResolver().openInputStream(uri);
            output = new FileOutputStream(dest);

            copyFileUsingStream(input, output);
        } finally {
            input.close();
            output.close();
        }
    }

    private static void copyFileUsingStream(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[1024];
        int read;
        while ((read = in.read(buf)) != -1) {
            out.write(buf, 0, read);
        }
    }
}