android - 对位图使用异步任务

标签 android url bitmap android-asynctask xamarin

我有一种方法可以将许多标记添加到 Google map 。

这是我的代码:

item = new MapLocation();
URL myurl = new URL("http://www.canninginc.co.nz/CanFindLocation/yes.png"); 
Bitmap bmp = BitmapFactory.DecodeStream(myurl.OpenConnection().InputStream);
item.icon = BitmapDescriptorFactory.FromBitmap(bmp);
item.Location = new LatLng (-41.227834, 174.812857);
item.Snippet = "Snippet2";
item.Title = "Title2";
item.ShowInfoWindowOnStartup = true;
_mapLocationList.Add(item);

我收到这个错误:

Exception of type 'Android.OS.NetworkOnMainThreadException' was thrown.

我做了一些研究,我认为我需要在获取位图时在另一个线程上运行操作。

我可以在这段代码上得到一些帮助吗? AsyncTask 是否返回位图,或者我如何将两者联系在一起?

提前致谢。

编辑

我正在使用 Xamarin,并将以下代码发布到一个新类中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;

namespace SimpleMapDemo
{
    class MyAsyncTask extends AsyncTask<String, Void, Bitmap>
    {
        @Override
        protected Bitmap doInBackground(String... urls) {
            if (urls.length > 0) {
                URL myurl = new URL("http://www.canninginc.co.nz/CanFindLocation/yes.png"); 
                return BitmapFactory.DecodeStream(myurl.OpenConnection().InputStream);
            }

            return null;
        }
        @Override
        protected void onPostExecute(Bitmap bmp) {
            super.onPostExecute(bmp);

            if (bmp != null) {
                item = new MapLocation();
                item.icon = BitmapDescriptorFactory.FromBitmap(bmp);
                item.Location = new LatLng (-41.227834, 174.812857);
                item.Snippet = "Snippet2";
                item.Title = "Title2";
                item.ShowInfoWindowOnStartup = true;
                _mapLocationList.Add(item);
            }
        }
    }
}

我遇到了很多错误。我是否将此代码放在了正确的区域?

最佳答案

在 AsyncTask 的 doInBackground 中获取输入流,如:

private class LoadImage extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected String doInBackground(String... params) {
       URL myurl = new URL("http://www.canninginc.co.nz/CanFindLocation/yes.png"); 
       Bitmap bmp = BitmapFactory.DecodeStream(myurl.OpenConnection().InputStream);
       return bmp;
    }

    @Override
    protected void onPostExecute(String result) {
        item.icon = BitmapDescriptorFactory.FromBitmap(bmp);
        item.Location = new LatLng (-41.227834, 174.812857);
        item.Snippet = "Snippet2";
        item.Title = "Title2";
        item.ShowInfoWindowOnStartup = true;
        _mapLocationList.Add(item);
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}

new LoadImage().execute();

您还可以将图片 URL 动态设置为:

 private class LoadImage extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected String doInBackground(String... params) {
       URL myurl = new URL(params[0]); 
       Bitmap bmp = BitmapFactory.DecodeStream(myurl.OpenConnection().InputStream);
       return bmp;
    }

    @Override
    protected void onPostExecute(String result) {
        item.icon = BitmapDescriptorFactory.FromBitmap(bmp);
        item.Location = new LatLng (-41.227834, 174.812857);
        item.Snippet = "Snippet2";
        item.Title = "Title2";
        item.ShowInfoWindowOnStartup = true;
        _mapLocationList.Add(item);
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}

new LoadImage().execute("http://www.canninginc.co.nz/CanFindLocation/yes.png");

关于android - 对位图使用异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21061811/

相关文章:

android - 如何获得EditText的默认橙色高光效果

android - react-native编译错误: The SDK Build Tools revision is too low for project

url - 回调中的 ShareThis 设置属性不起作用

java - 连续读取网站流

java - 在Android中为OpenGL ES从位图创建纹理

c++ - 如何使用 OpenGL 创建位图的实体挤压?

android - 从命令行构建应用程序

java - Android上下文菜单自定义定位?

url - 从 url 中删除查询字符串而不刷新页面?

android - StaticLayout 高度测量关闭