Android Bitmap Load downloadImageToSd(String urlSource, String pathDest)

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

Description

Method

Declaration

public static Boolean downloadImageToSd(String urlSource,
        String pathDest) 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 ww w .  jav a 2s  .  co  m*/

        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. decodeFile(File f, int size)
  2. decodeFromDescriptor( FileDescriptor descriptor)
  3. decodeFromDescriptor( FileDescriptor descriptor, int reqWidth, int reqHeight)
  4. getBitmap(Context c, String uri, int minsizeDP)
  5. getImageFromAssetFile(Context context, String fileName)
  6. downloadImageToSd(String urlSource, String pathDest, String referer)
  7. getResizedBitmap(Uri imageUri, Activity activity)
  8. downSampleBitmap(Uri uri, Activity act, Boolean needRotate)
  9. getBitmap(Context context, int resId)