Back to project page Shutterbug.
The source code is released under:
* Copyright (c) 2012, Applidium * All rights reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met...
If you think the Android project Shutterbug listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.applidium.shutterbug.downloader; //from ww w . jav a 2s .c o m import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import android.os.AsyncTask; import com.applidium.shutterbug.utils.DownloadRequest; public class ShutterbugDownloader { public interface ShutterbugDownloaderListener { void onImageDownloadSuccess(ShutterbugDownloader downloader, InputStream inputStream, DownloadRequest downloadRequest); void onImageDownloadFailure(ShutterbugDownloader downloader, DownloadRequest downloadRequest); } private String mUrl; private ShutterbugDownloaderListener mListener; private byte[] mImageData; private DownloadRequest mDownloadRequest; private final static int TIMEOUT = 30000; private AsyncTask<Void, Void, InputStream> mCurrentTask; public ShutterbugDownloader(String url, ShutterbugDownloaderListener listener, DownloadRequest downloadRequest) { mUrl = url; mListener = listener; mDownloadRequest = downloadRequest; } public String getUrl() { return mUrl; } public ShutterbugDownloaderListener getListener() { return mListener; } public byte[] getImageData() { return mImageData; } public DownloadRequest getDownloadRequest() { return mDownloadRequest; } public void start() { mCurrentTask = new AsyncTask<Void, Void, InputStream>() { @Override protected InputStream doInBackground(Void... params) { HttpGet request = new HttpGet(mUrl); request.setHeader("Content-Type", "application/x-www-form-urlencoded"); try { URL imageUrl = new URL(mUrl); HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection(); connection.setConnectTimeout(TIMEOUT); connection.setReadTimeout(TIMEOUT); connection.setInstanceFollowRedirects(true); InputStream inputStream = connection.getInputStream(); return inputStream; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(InputStream inputStream) { if (isCancelled()) { inputStream = null; } if (inputStream != null) { mListener.onImageDownloadSuccess(ShutterbugDownloader.this, inputStream, mDownloadRequest); } else { mListener.onImageDownloadFailure(ShutterbugDownloader.this, mDownloadRequest); } } }.execute(); } public void cancel() { if (mCurrentTask != null) { mCurrentTask.cancel(true); } } }