I'm trying to set the source of an ImageView to the image located at a Google Static Map's URL. An example of this url would be this... LINK
http://maps.google.com/maps/api/staticmap?center=37.386052,-122.083851&zoom=13&markers=size:mid%7Ccolor:blue%7C37.386052,-122.083851&path=color:0x0000FF80%7Cweight:5%7C37.40276,-122.06360&size=220x150&sensor=false
I have read multiple StackOverflow posts about this issue, and all of those questions have been answered with pieces of code that work for other people but for some reason not me. Here is my code. (I left out pieces that do not have to do with the issue). The NullPointerException occurs at line mapView.setImageBitmap(result);
Main Class
public class CoffeeResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
String userChoice = (String) getIntent().getExtras().get("userChoice");
//CALL THE ASYNC TASK HERE
new RetreiveSearchResults().execute(userChoice);
}
class RetreiveSearchResults extends AsyncTask<String, Void, Void> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(CoffeeResultActivity.this);
progressDialog.setMessage("Searching for caffeine...");
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected Void doInBackground(String... terms) {
/*
.........
*/
try {
//GETS IMAGE
ImageLoadingTask task = new ImageLoadingTask();
task.execute("http://maps.google.com/maps/api/staticmap?center=37.386052,-122.083851&zoom=13&markers=size:mid%7Ccolor:blue%7C37.386052,-122.083851&path=color:0x0000FF80%7Cweight:5%7C37.40276,-122.06360&size=220x150&sensor=false");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
listAdapter = new ExpandableListAdapter(CoffeeResultActivity.this, businessNames, businessInfo);
listAdapter.notifyDataSetChanged();
// setting list adapter
expListView.setAdapter(listAdapter);
progressDialog.dismiss();
}
}
public class ImageLoadingTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... stringURL) {
Bitmap bmp = null;
try {
URL url = new URL(stringURL[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
bmp = BitmapFactory.decodeStream(is, null, options);
} catch (Exception e) {
e.printStackTrace();
}
return bmp;
}
@Override
protected void onPostExecute(Bitmap result) {
ImageView mapView = (ImageView) findViewById(R.id.imageView1);
mapView.setImageBitmap(result);
super.onPostExecute(result);
}
}
}
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >
<TextView
android:id="@+id/lblListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:linksClickable="true"
android:paddingLeft="8dp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
Can anyone shed some light into this?
why don't you use image loader library as AQuery. you can find a .jar library that you can directly paste on your libs folder. it supports caching too.
an example usage to load an image from url to imageview.