1

我有一个异步任务,它正在从网络(谷歌地图标记)下载图像。是否可以替换我的硬编码可绘制标记,R.drawable.maps1

我认为我正确接收了图像,但我被困在这里。任何帮助深表感谢。

图片下载任务

public static class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> {

    private String url;

    public ImageDownloadTask(String url) {
        this.url = url;
    }

    protected Bitmap doInBackground(Void... params) {

        final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        final HttpGet getRequest = new HttpGet(url);

        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                Log.w("ImageDownloadTask", "Error " + statusCode
                        + " while retrieving bitmap from " + url);
                return null;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = entity.getContent();
                    final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    Log.w("ImageDownloadTask", "success! "
                            + url);
                    return bitmap;
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            getRequest.abort();
            Log.w("ImageDownloadTask", "Error while retrieving bitmap from "
                    + url);
        } finally {
            if (client != null) {
                client.close();
            }
        }
        return null;
    }

    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null) {

        }

    }
}

添加标记

public void addOverlayItem(double latitude, double longitude, String title, String description)
{
    List<Overlay> mapOverlays = mapView.getOverlays();


    //AsyncTasks.ImageDownloadTask imgTask = new AsyncTasks.ImageDownloadTask("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|c6c6c6");
    //imgTask.execute();

            //can i make use of the bitmap to replace "R.drawable.maps1" below?

    Drawable drawable = this.getResources().getDrawable(R.drawable.maps1);

    OverlayItems itemizedoverlay = new OverlayItems(drawable, this);

    GeoPoint gp = new GeoPoint((int)(latitude * 1E6), (int)(longitude * 1E6));

    OverlayItem overlayitem = new OverlayItem(gp, title, description);

    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);

}

当前解决方案

new BitmapDrawable(getResources(), imgTask.execute().get());

这似乎有效,但不能很聪明,对吧?有没有办法在不使用的情况下做到这一点.get()

4

0 回答 0