samedi 28 mars 2015

Android cannot connect HttpURLConnection to get Bitmap

I'm trying to get Bitmap from URL. I use the methos:



public static Bitmap getBitmapFromURL(String src) {
try {

URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}


But I cannot pass the line: "connection.connect();". Is there sth which I'm missing? How can I connect()?


My string src = "http://ift.tt/19cwTCl"


I read that i should " This should be used in AsyncTask derived Class.". How to do it?


Aucun commentaire:

Enregistrer un commentaire