Unable to add icon to Marker , Map V2 Android

流过昼夜 提交于 2019-12-08 12:56:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!