Download the file located at the provided internet url using the URL class - Android Network

Android examples for Network:HTTP

Description

Download the file located at the provided internet url using the URL class

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import android.content.Context;

import android.net.Uri;

import android.util.Base64;
import android.util.Log;

public class Main {
    /**//w  w  w .j  a  va 2s . com
     * Used for debugging.
     */
    static final String TAG = "DownloadActivity";

    /**
     * Download the file located at the provided internet url using
     * the URL class, store it on the android file system using
     * openFileOutput(), and return the path to the file on disk.
     *
     * @param context   the context in which to write the file
     * @param uri       the web url
     * 
     * @return          the path to the downloaded file on the file system
     */
    public static String downloadFile(Context context, Uri uri) {

        try {
            // Create a temp file.
            final File file = getTemporaryFile(context, uri.toString());
            Log.d(TAG, "    downloading to " + file);

            // Download the contents at the URL, which should
            // reference an image.
            final InputStream in = (InputStream) new URL(uri.toString())
                    .getContent();
            final OutputStream os = new FileOutputStream(file);

            // Copy the contents of the downloaded image to the
            // temp file.
            copy(in, os);
            in.close();
            os.close();

            // Return the pathname of the temp file.
            return file.getAbsolutePath();
        } catch (Exception e) {
            Log.e(TAG, "Exception while downloading. Returning null.");
            Log.e(TAG, e.toString());
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Create a temp file to store the result of a download.
     * 
     * @param context
     * @param url
     * @return
     * @throws java.io.IOException
     */
    static private File getTemporaryFile(final Context context,
            final String url) throws IOException {

        // This is what you'd normally call to get a unique temporary
        // file, but for testing purposes we always name the file the
        // same to avoid filling up student phones with numerous
        // files!
        // return context.getFileStreamPath(Base64.encodeToString(url.getBytes(),
        //                                  Base64.NO_WRAP)
        //                                  + System.currentTimeMillis());

        return context.getFileStreamPath(Base64.encodeToString(
                url.getBytes(), Base64.NO_WRAP));
    }

    /**
     * Copy the contents of an InputStream into an OutputStream.
     * 
     * @param in
     * @param out
     * @return
     * @throws java.io.IOException
     */
    static public int copy(final InputStream in, final OutputStream out)
            throws IOException {
        final int BUFFER_LENGTH = 1024;
        final byte[] buffer = new byte[BUFFER_LENGTH];
        int totalRead = 0;
        int read = 0;

        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
            totalRead += read;
        }

        return totalRead;
    }
}

Related Tutorials