问题
Here is how i am adding marker to map
map.addMarker(new MarkerOptions()
.position(model.getLatLongfromService())
.title(model.getCoupon_name())
.snippet(model.getCoupon_id())
.icon(BitmapDescriptorFactory.fromFile(DataHolder.imageUrl
+ model.getCoupon_image())));
I am getting coupon_image in this format : http://www.xyz.com/coupon21.jpg**
I am getting this error when u run my app.
java.lang.IllegalArgumentException: File http://test.xyz.de/uploads/company_logo/sample-logo-110x60.jpg contains a path separator
Can anyone help me to understand what the problem is ?
Thanks, Rakesh
回答1:
I think the problem is that method BitmapDescriptorFactory.fromFile uses parameter String fileName, which represents name of the file(image) to load. You supply image's http url (http://test.xyz.de/uploads/company_logo/sample-logo-110x60.jpg) instead of it.
You probably need to download the image first and then use BitmapDescriptorFactory.fromBitmap;
EDIT: To download image, you can use some AsyncTask like this for example:
AsyncTask<String, Void, Bitmap> loadImageTask = new AsyncTask<String, Void, Bitmap>(){
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bmImg = null;
try {
URL url = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
}
catch (IOException e)
{
e.printStackTrace();
bmImg = null;
}
return bmImg;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
// TODO: do what you need with resulting bitmap - add marker to map
}
};
then don't forget to execute asynctask with proper parameter - String array containing url of image to download:
loadImageTask.execute(new String[]{yourImageUrl});
来源:https://stackoverflow.com/questions/15549031/unable-to-add-icon-to-marker-map-v2-android