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();}