This article describes the packaging method of Android programming to implement image library. Share it for your reference, as follows:
When you are working on Android applications, you often need to get pictures from the network. They all obtain them through URLs. However, if there is image data locally, it is not faster to obtain data from the local area. I encountered this problem at work, so I used a mapping relationship between a URL and a local image. Get it from the local area first. If the local area is not obtained from the network, this method takes into account multi-threading problems. Welcome everyone to discuss together!
public class PictureLibrary { /* * Operation of the picture library */ File file; URL url; HttpURLConnection conn; InputStream is; FileOutputStream fos; private Lock lock = new ReentrantLock(); private Condition downFile = (); // Download data to local operation through URL private String toLocalFile(String strURL) { String fileName = (strURL); String path = () + "/" + EcologicalTourism.FILE_PATH + "/images/" + fileName; return path; } // Download data to local temporary file via URL private String toLocalFileTemp(String strURL) { String s = (strURL); String fileName = s+"temp"; String path_url = () + "/" + EcologicalTourism.FILE_PATH + "/tempimages/" + fileName; return path_url; } /* * Save the image locally and return the local url (this function is blocking) * main * @parameters: strURL, the parameter is the URL of the image. Return value: The image is temporarily stored in the local SD card. * The function's job is to obtain the image from the Internet, store it in local storage, and return the file path of the local storage for direct use by the caller. If the file already exists locally, return it directly * If the file is not local, download it directly from the server and the function blocks. */ public String getReadSD(String strURL) { ("test", "The address you got to the network is:" + strURL); String strLocalFile = toLocalFile(strURL); //k: Convert server URL to local URL String strLocalFileTemp = toLocalFileTemp(strURL); //k: Convert server URL to local temporary URL while (true) { File file = new File(strLocalFile); ("test", "The local file is:" + strLocalFile); File tfile = new File(strLocalFileTemp); ("test", "The temporary file is:" + strLocalFileTemp); // 1 Lock (); if (()) { // 2if local file exists // Unlock // Return to the local path (); ("test", "Return to local path:" + file); return strLocalFile; } else if (()) { // if the corresponding temporary file exists // Unlock (); try { // Sleep (); } catch (Exception e) { (); ("test", "e Exception 1" + e); } } else { try { // Create the corresponding temporary file (); } catch (IOException e) { ("test", "e Exception 2" + e); } // Unlock (); // Download the file contents into the temporary file downURL2(strURL, strLocalFile); // Lock (); // Modify the name of the temporary file to the local file name (file); // Unlock (); } } } private void downURL2(String strURL, String strLocalFileTemp) { // TODO Auto-generated method stub URL url; try { url = new URL(strURL); HttpURLConnection conn = (HttpURLConnection) (); (5000); ("GET"); (true); if (() == 200) { InputStream is = (); FileOutputStream fos = new FileOutputStream(strLocalFileTemp); byte[] buffer = new byte[1024]; int len = 0; while ((len = (buffer)) != -1) { (buffer, 0, len); } (); (); // Return a URI object } } catch (MalformedURLException e) { // TODO Auto-generated catch block (); } catch (ProtocolException e) { // TODO Auto-generated catch block (); } catch (IOException e) { // TODO Auto-generated catch block (); } } /* * Blocking download url to file toFile */ private boolean downURL(String strURL, String toFile) { URL url; try { url = new URL(strURL); HttpURLConnection httpUrl = (HttpURLConnection) url .openConnection(); ("GET"); int fileSize = ();// File size ();// Close the connection int threadSize = 6;// 6 threads are set by default threadSize = fileSize % threadSize == 0 ? threadSize : threadSize + 1; int currentSize = fileSize / threadSize; // Download size per thread String dowloadir = () + "/" + EcologicalTourism.FILE_PATH + "/images/"; File dir = new File(dowloadir); if (!()) { (); } File file = new File(dir, toFile); RandomAccessFile randomFile = new RandomAccessFile(file, "rw"); (fileSize);// Specify the size of the file for (int i = 0; i < threadSize; i++) { int startposition = i * currentSize;// Where each thread starts writing to the file RandomAccessFile threadFile = new RandomAccessFile(file, "rw"); ("syso", "The content of toFile is:" + toFile); (startposition); new DownLoadThread(i, currentSize, threadFile, startposition, url).start(); } } catch (Exception e) { (); ("syso", "Download download failed" + e); } return true; } /** * Implement thread download * */ private static class DownLoadThread extends Thread { @SuppressWarnings("unused") private int threadId;// Thread number private int currentSize;// Size of each thread private RandomAccessFile threadFile; // Each thread needs to be written to the file class private int startposition;// Where each thread starts writing to the file private URL url; //Network address public DownLoadThread(int threadId, int currentSize, RandomAccessFile threadFile, int startposition, URL url) { = threadId; = currentSize; = threadFile; = startposition; = url; } public void run() { try { HttpURLConnection httpUrl = (HttpURLConnection) url .openConnection(); ("GET"); ("range", "bytes=" + startposition + "-");// Specify the location of the server InputStream is = (); byte[] data = new byte[1024]; int len = -1; int threadFileSize = 0; while ((threadFileSize < currentSize) && ((len = (data)) != -1)) { (data, 0, len); threadFileSize += len; } (); (); } catch (Exception e) { (); } } } /** * Get pictures from this cache */ public Bitmap getBitmapFromCache(String imageURL) { // String bitmapName = (("/") + 1); String bitmapName = (imageURL); File cacheDir = new File(() + "/" + EcologicalTourism.FILE_PATH + "/images/"); File[] cacheFiles = (); int i = 0; if(null!=cacheFiles){ for(; i<;i++){ if((cacheFiles[i].getName())){ break; } } if(i < ) { return (() + "/" + EcologicalTourism.FILE_PATH + "/images/" + bitmapName); } } return null; }
I hope this article will be helpful to everyone's Android programming design.