In Android, Textview displays html text 3-[Textview displays network images]

Source: Internet
Author: User

In the previous article, I talked about Textview display of local images. For details, see Textview display in Android with html text 2 ------- [Textview display of local images]

One problem left over from the previous article is: displaying network images. I use the Android system to display images. If the images are large, the application will become stuck, it must be because the main thread is used to obtain network images. However, if I use a system running above android4.0, the image cannot be displayed, but only a small box is displayed.

The cause is that an error is reported during execution on the 4.0 system. The exception is android. OS. the NetworkOnMainThreadException was found in the document. It turns out that the 4.0 system does not allow the main thread (UI thread) to access the network, resulting in exceptions. To put it bluntly, access to the network on the main thread will cause the main thread to be suspended and the system will not allow it to be used.

When we see that Android4.0 does not allow the main thread (UI thread) to access the network, we immediately remember that we cannot use the main thread to access the network. We can open another thread and download the image to the local SD card, and then assign the value to TextView. I am not in a rush to come to the code. I am taking this logic into consideration:Get image path -- Download image asynchronously -- assign a value to Textview after download.

Now, I want to practice it myself ...... as a result, I simply wrote the DownLoadUtils File Download class. There are four events: Start download, download in progress (return Progress). After the download is complete, the download error occurs! The specific code will not be pasted out. You can write one by yourself and search all the code for downloading the file! The download class uses threads and Handler. I will talk about this in the next article.

The following code processes the Activity page:

View Code

Private TextView tView; private DownLoadUtils downLoadUtils; // save the file path private final String path = "/mnt/sdcard/downimg "; // set the text value String sText = "test image information: <br> 

Next, let's briefly introduce the above Code. The most important thing is that there are two points: sText is assigned Textview for the first time, in Html. the reload method of ImageGetter checks whether the image file has been downloaded. If the image file has been downloaded, it directly reads the image file in the SD card. For example, the Textview mentioned in the previous article shows the local image,

// Obtain the local file and return Drawable drawable = Drawable. createFromPath (fileString); // sets the image border drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight ());

Enable a download thread if there is no download

// Start a new thread to download
DownLoadUtils. download (source, path + String. valueOf (source. hashCode ()));

The second focus is to listen for download completion events. After the download is complete, assign a new value to Textview.

View Code

OnDownloadListener onDownloadListener = new OnDownloadListener () {// download progress public void onDownloadUpdate (DownLoadUtils manager, int percent) {// TODO Auto-generated method stub Log. I ("DEBUG", percent + "");} // download failed public void onDownloadError (DownLoadUtils manager, Exception e) {// TODO Auto-generated method stub} // start to download public void onDownloadConnect (DownLoadUtils manager) {// TODO Auto-generated method stub Log. I ("DEBUG", "Start //");} // complete the download of public void onDownloadComplete (DownLoadUtils manager, Object result) {// TODO Auto-generated method stub Log. I ("DEBUG", result. toString (); // replace the value of sTExt, that is, change the network path of the image to the path of the local SD card image (the earliest idea is that you do not need to do this) // sText. replace (result. toString (), path + String. valueOf (result. hashCode (); // value it to Textview tView again. setText (Html. fromHtml (sText, imageGetter, null ));}};

After this is done, the network image can be displayed in Textview. When the network is normal, you only need to download the same image once, which can save the traffic on your mobile phone.

I also have a solution that does not need to assign two values to Textview, that is, first parse the image path, then download the image, and finally assign the value to Textview. The truth is the same, the previous method is to parse the path of the image by reloading the method and then download the image. There is only one more value assignment, without any impact. If you have a good idea, you can also introduce it.

The above points only represent my personal opinions.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.