Created
September 9, 2015 07:08
-
-
Save Folyd/22f035678a03da6df371 to your computer and use it in GitHub Desktop.
Simple Bitmap AsyncTask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.os.AsyncTask; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class SimpleBitmapAsyncTask extends AsyncTask<String, Void, Bitmap> { | |
@Override | |
protected Bitmap doInBackground(String... params) { | |
Bitmap bitmap = null; | |
InputStream input; | |
HttpURLConnection connection; | |
URL url; | |
try { | |
url = new URL(params[0]); | |
connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("GET"); | |
connection.setConnectTimeout(8000); | |
connection.setDoInput(true); | |
connection.connect(); | |
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { | |
return null; | |
} | |
input = connection.getInputStream(); | |
bitmap = BitmapFactory.decodeStream(input); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return bitmap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment