安卓html.fromhtml从网页加载图像 [英] android html.fromhtml to load image from web

查看:220
本文介绍了安卓html.fromhtml从网页加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何html.fromhtml加载从Web图像,并设置成ImageView的?

how can we html.fromhtml to load image from web and set into imageview ?

推荐答案

异步图片下载

首先要做的是确保你请求允许清单文件中下载图像。

First thing to do is to make sure you request permission to download images inside the manifest file.

<uses-permission android:name="android.permission.INTERNET" />

那么,从我们需要打开HTTP连接,下载并返回到图像的网页下载的图像。这个方法应该去的活动中。

Then, to download an image from the web we need to open an HTTP connection, download and return the image. This method should go inside the activity.

private Bitmap DownloadImage(String URL)

然后我们会再下载的图像添加到ImageView的

Then we would then add the downloaded image to the ImageView

Bitmap bitmap = DownloadImage("http://www.streetcar.org/mim/cable/images/cable-01.jpg");
ImageView  img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);

然而,这不是异步的。

However, this isn’t asynchronous.

通常情况下,我们将创建一个线程做一些后台工作,但一个线程不能更新视图它并没有创造。

Normally we would create a thread to do some background work but a thread can’t update a view it didn’t create.

要解决这个问题,我们可以使用 AsyncTask的。我已经写了延伸AsyncTask的这个小的内部类。

To solve this problem we can use AsyncTask. I’ve written this little inner class that extends AsyncTask.

class DownloadImagesTask extends AsyncTask<String, Integer, Bitmap> {

private int imageViewID;

    protected void onPostExecute(Bitmap bitmap1) {
    setImage(imageViewID, bitmap1);
}

    public void setImageId(int imageViewID) {
        this.imageViewID = imageViewID;
    }

    @Override
    protected Bitmap doInBackground(String... url) {
        Bitmap bitmap1 = 
            DownloadImage(url[0]);
        return bitmap1;
    }

}

使用的AsyncTask的三种类型

The three types used by AsyncTask are

  1. PARAMS,参数的类型 在执行发送给任务。
  2. 在进步,在后台计算期间发表的进度的单位类型。
  3. 结果,背景计算的结果的类型。
  1. Params, the type of the parameters sent to the task upon execution.
  2. Progress, the type of the progress units published during the background computation.
  3. Result, the type of the result of the background computation.

所以,更换旧code,我们现在可以使用

So to replace the old code we can now use

DownloadImagesTask task1 = new DownloadImagesTask();
task1.setImageId(R.id.img1);
task1.execute("http://assets.devx.com/articlefigs/39810_1.jpg");

这得到了很多的时间比我的计划。在codeS并不完美,但我希望这有助于你。

This got a lot longer than I planned. The codes not perfect but I hope it’s helped you.

请注意:这是基于连接到网络在DevX

Note: This was is based on Connecting to the web at DevX

引用

  • 连接到网络: <一href="http://www.devx.com/wireless/Article/39810/1954">http://www.devx.com/wireless/Article/39810/1954
  • 的AsyncTask: <一href="http://developer.android.com/reference/android/os/AsyncTask.html">http://developer.android.com/reference/android/os/AsyncTask.html
  • Connecting to the web : http://www.devx.com/wireless/Article/39810/1954
  • AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html

这篇关于安卓html.fromhtml从网页加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆