Android Bitmap Load downloadImageToSd(String urlSource, String pathDest, String referer)

Here you can find the source of downloadImageToSd(String urlSource, String pathDest, String referer)

Description

download Image To Sd

Declaration

public static Boolean downloadImageToSd(String urlSource,
            String pathDest, String referer) throws IOException 

Method Source Code

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    /******************** Method ********************/
    public static Boolean downloadImageToSd(String urlSource,
            String pathDest) throws IOException {
        HttpURLConnection mHttpURLConnection = null;
        FileOutputStream mFileOutputStream = null;
        BufferedOutputStream mBufferedOutputStream = null;
        BufferedInputStream mBufferedInputStream = null;
        File imageFile = null;//from  w  w w  .j ava  2s  .  c om

        URL url = new URL(urlSource);
        mHttpURLConnection = (HttpURLConnection) url.openConnection();
        mHttpURLConnection.setConnectTimeout(5 * 1000);
        mHttpURLConnection.setReadTimeout(15 * 1000);
        mHttpURLConnection.setDoInput(true);
        mHttpURLConnection.setDoOutput(true);
        mBufferedInputStream = new BufferedInputStream(
                mHttpURLConnection.getInputStream());
        imageFile = new File(pathDest);
        mFileOutputStream = new FileOutputStream(imageFile);
        mBufferedOutputStream = new BufferedOutputStream(mFileOutputStream);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = mBufferedInputStream.read(buffer)) != -1) {
            mBufferedOutputStream.write(buffer, 0, length);
            mBufferedOutputStream.flush();
        }
        return true;

    }

    public static Boolean downloadImageToSd(String urlSource,
            String pathDest, String referer) throws IOException {
        HttpURLConnection mHttpURLConnection = null;
        FileOutputStream mFileOutputStream = null;
        BufferedOutputStream mBufferedOutputStream = null;
        BufferedInputStream mBufferedInputStream = null;
        File imageFile = null;

        URL url = new URL(urlSource);
        mHttpURLConnection = (HttpURLConnection) url.openConnection();
        mHttpURLConnection.setRequestProperty("referer", referer);
        mHttpURLConnection.setConnectTimeout(5 * 1000);
        mHttpURLConnection.setReadTimeout(15 * 1000);
        mHttpURLConnection.setDoInput(true);
        mHttpURLConnection.setDoOutput(true);
        mBufferedInputStream = new BufferedInputStream(
                mHttpURLConnection.getInputStream());
        imageFile = new File(pathDest);
        mFileOutputStream = new FileOutputStream(imageFile);
        mBufferedOutputStream = new BufferedOutputStream(mFileOutputStream);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = mBufferedInputStream.read(buffer)) != -1) {
            mBufferedOutputStream.write(buffer, 0, length);
            mBufferedOutputStream.flush();
        }
        return true;

    }
}

Related

  1. decodeFromDescriptor( FileDescriptor descriptor)
  2. decodeFromDescriptor( FileDescriptor descriptor, int reqWidth, int reqHeight)
  3. getBitmap(Context c, String uri, int minsizeDP)
  4. getImageFromAssetFile(Context context, String fileName)
  5. downloadImageToSd(String urlSource, String pathDest)
  6. getResizedBitmap(Uri imageUri, Activity activity)
  7. downSampleBitmap(Uri uri, Activity act, Boolean needRotate)
  8. getBitmap(Context context, int resId)
  9. getBitmap(String photoId)