Android html.fromhtml download image from the Internet

how can we html.fromhtml download an image from the internet and install it in an image?

+3
source share
1 answer

Download asynchronous images

First of all, make sure you request permission to upload images inside the manifest file.

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

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

private Bitmap DownloadImage(String URL)

Then we add the uploaded image to 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 is not asynchronous.

Usually we created a thread to do background work, but the thread could not update the view that it did not create.

, AsyncTask. Ive , 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,

  • , .
  • , , .
  • , .

, ,

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

, . , , .

. DevX

+11

Source: https://habr.com/ru/post/1727091/


All Articles