Try using this, which implements an AsyncTask to do stuff in the background (needed for Android 4.0+):
class loadimage extends AsyncTask<Void, Void, Void> {
ProgressDialog pdLoading = new ProgressDialog(YourClass.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
pdLoading.setMessage("Loading Image...");
pdLoading.show();
}
@Override
protected void doInBackground(Void... params) {
String stringurl = "http://imgur.com/1awAsRh"
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(stringurl).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pdLoading.dismiss();
// do what you want with your bitmap
return null;
}
}
And then call it like this in your onCreate
:
new loadimage().execute();
You also need to make sure you have this in your manifest:
<uses-permission android:name="android.permission.INTERNET" />