Android Bitmap Load download(String url, String fileName)

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

Description

Downloads file from specified URL and stores as filename mentioned as parameter, in default external storage directory.

Parameter

Parameter Description
url the url
fileName the file name

Exception

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

Declaration

public static void download(String url, 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 {
    /**//  www .j  a v a  2s . c om
     * 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. decodeSampledBitmapFromBytes(byte[] res, int reqWidth, int reqHeight)
  2. decodeSampledBitmapFromBytesForCurrentScreen( byte[] res, Context ctxt)
  3. decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
  4. decodeSampledBitmapFromUrl(URL url, int reqWidth, int reqHeight)
  5. decodeSampledBitmapStreamForSize(InputStream is, int reqWidth, int reqHeight)
  6. fetchImage(final Context context, final String url, final BitmapFactory.Options decodeOptions, final Object cookie, final OnFetchCompleteListener callback)
  7. fetchImage(final Context context, final String url, final OnFetchCompleteListener callback)
  8. getBitmap(Resources resources, int resourceId)
  9. getBitmap(String url, Context context, String newPicName)