How to obtain network images in Android (if there is a cache in the mobile phone, it will be obtained from the cache)

Source: Internet
Author: User
Recently, my work has been relatively idle. In addition to using the company's iMac machine to learn iPhone, it is necessary to summarize the difficulties in previous projects. For the difficulties in Android development, first, the processing of content obtained through the network, and second, the uidesign. For me, I am particularly troubled by the uidesign aspect. The company's development focuses on iPhones. After all, the popularity of iPhones in Hong Kong is relatively high (the sales price seems to be the lowest in the world ), in order to imitate the tabbar of the iPhone, the processing method of tabactivity + activitygroup does not know how many problems are solved. Fortunately, all of them are solved one by one.

The method for obtaining network images (if there is a cache in the mobile phone, it will be obtained from the cache). I previously wrote, compared to the original:

Imageview mimageview = (imageview) This. findviewbyid (R. id. imageview); string ImagePath = getimagepath (context, photourl); // context: context, photourl: mimageview of the image URL. setimagebitmap (bitmapfactory. decodefile (ImagePath ));

// Obtain the network image. If there is any image in the cache, obtain the public static string getimagepath (context, string URL) {If (url = NULL) Return ""; string ImagePath = ""; string filename = ""; // obtain the image file name and suffix if (URL! = NULL & URL. Length ()! = 0) {filename = URL. substring (URL. lastindexof ("/") + 1);} // path of the image stored locally on the mobile phone. Note: If filename is empty, ImagePath = context. getcachedir () + "/" + filename; log. I (TAG, "ImagePath =" + ImagePath); file = new file (context. getcachedir (), filename); // save the file, if (! File. exists () {log. I (TAG, "file does not exist"); try {byte [] DATA = readinputstream (getrequest (URL); Bitmap bitmap = bitmapfactory. decodebytearray (data, 0, Data. length); bitmap. compress (compressformat. JPEG, 100, new fileoutputstream (File); ImagePath = file. getabsolutepath (); log. I (TAG, "ImagePath: file. getabsolutepath () = "+ ImagePath);} catch (exception e) {log. E (TAG, E. tostring () ;}} return ImagePath;} // get ImagePath () ends.

Public static inputstream getrequest (string path) throws exception {URL url = new URL (PATH); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setrequestmethod ("get"); Conn. setconnecttimeout (5000); // 5 seconds if (Conn. getresponsecode () = 200) {return Conn. getinputstream ();} return NULL ;}

public static byte[] readInputStream(InputStream inStream) throws Exception{        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();        byte[] buffer = new byte[4096];        int len = 0;        while( (len = inStream.read(buffer)) != -1 ){            outSteam.write(buffer, 0, len);        }        outSteam.close();        inStream.close();        return outSteam.toByteArray();}

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.