Android URL Download download(String url, String directoryName, String fileName)

Here you can find the source of download(String url, String directoryName, String fileName)

Description

Downloads file from specified URL and stores as filename mentioned as parameter in directory name specified use this method when you want to download your imahe into a specific directory and not in default external storage.

Parameter

Parameter Description
url the url
directoryName the directory name
fileName the file name

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Return

absolute path of saved file

Declaration

public static String download(String url, String directoryName,
        String fileName) throws IOException 

Method Source Code

//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.os.Environment;

public class Main {
    /**//ww  w.j  a  v a  2 s  . c  o  m
     * Downloads file from specified URL and stores as filename mentioned as
     * parameter, in default external storage directory.
     *
     * @param url the url
     * @param fileName the file name
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void download(String url, String fileName)
            throws IOException {
        Bitmap bmp = getFromUrl(url);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 0, bytes);

        // you can create a new file name "test.jpg" in sdcard folder.
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + fileName);
        f.createNewFile();
        // write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());

        // remember close de FileOutput
        fo.close();
    }

    /**
     * Downloads file from specified URL and stores as filename mentioned as
     * parameter in directory name specified use this method when you want to
     * download your imahe into a specific directory and not in default external
     * storage.
     *
     * @param url the url
     * @param directoryName the directory name
     * @param fileName the file name
     * @return absolute path of saved file
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static String download(String url, String directoryName,
            String fileName) throws IOException {
        Bitmap bmp = getFromUrl(url);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 0, bytes);

        File dir = new File(Environment.getExternalStorageDirectory()
                + File.separator + directoryName);

        if (!dir.exists()) {
            dir.mkdirs();
        }

        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + directoryName + File.separator
                + fileName);
        f.createNewFile();
        // write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());

        // remember close de FileOutput
        fo.close();

        return f.getAbsolutePath();
    }

    /**
     * Gets the from url.
     * 
     * @param url
     *            the url
     * @return the from url
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Bitmap getFromUrl(String url) throws IOException {
        URL myFileUrl = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        HttpURLConnection conn = (HttpURLConnection) myFileUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();

        Bitmap bmImg = BitmapFactory.decodeStream(is);

        return bmImg;
    }
}

Related

  1. downloadFile(String downloadUrl, String outputFilePath)
  2. downloadFile(URL url, OutputStream output)
  3. downloadHtmlPage(String pageUrl)
  4. downloadUrl(String urlString)