In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the method of Android to solve the problem of downloading a large number of pictures?" in the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Strong, soft, weak, and virtual references to objects
In order to control the life cycle of an object more flexibly, you need to know the four levels of object reference, from high to low: strong reference, soft reference, weak reference and virtual reference.
Remarks: the difference between these four categories:
⑴ strong reference (StrongReference)
Strong references are the most commonly used references. If an object has a strong reference, the garbage collector will never reclaim it. When the memory space is insufficient, the Java virtual machine would rather throw an OutOfMemoryError error to make the program terminate abnormally, rather than solve the problem of insufficient memory by randomly recycling objects with strong references.
⑵ soft reference (SoftReference)
If an object has only soft references, the garbage collector will not reclaim it if there is enough memory space; if there is not enough memory space, it will reclaim the memory of those objects. As long as the garbage collector does not reclaim it, the object can be used by the program. Soft references can be used to implement memory-sensitive caching (shown below). A soft reference can be used in conjunction with a reference queue (ReferenceQueue), and if the object referenced by the soft reference is reclaimed by the garbage collector, the Java virtual machine will add the soft reference to the reference queue associated with it.
⑶ weak reference (WeakReference)
The difference between weak references and soft references is that objects with only weak references have a shorter life cycle. When the garbage collector thread scans the memory area under its jurisdiction, once an object with only weak references is found, its memory will be reclaimed regardless of whether the current memory space is sufficient or not. However, because the garbage collector is a low-priority thread, objects with only weak references will not necessarily be found quickly. A weak reference can be used in conjunction with a reference queue (ReferenceQueue), and if the object referenced by the weak reference is garbage collected, the Java virtual machine adds the weak reference to the reference queue associated with it.
⑷ virtual reference (PhantomReference)
As the name implies, "virtual reference" is nonexistent, and unlike other references, virtual reference does not determine the life cycle of the object. If an object holds only a virtual reference, it can be reclaimed by the garbage collector at any time as if it had no reference at all. Virtual references are mainly used to track the activity of objects being reclaimed by the garbage collector. One difference between virtual references and soft and weak references is that virtual references must be used in conjunction with reference queues (ReferenceQueue). When the garbage collector is ready to recycle an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory.
ReferenceQueue queue = new ReferenceQueue (); PhantomReference pr = new PhantomReference (object, queue)
The program can know whether the referenced object is going to be garbage collected by determining whether a virtual reference has been added to the reference queue. If the program finds that a virtual reference has been added to the reference queue, it can take the necessary action before the memory of the referenced object is reclaimed
two。 The working principle and function of soft reference
If an object has only soft references, the garbage collector will not reclaim it if there is enough memory space; if there is not enough memory space, it will reclaim the memory of those objects. As long as the garbage collector does not reclaim it, the object can be used by the program.
Soft references can be used to implement memory-sensitive caching, and soft references can be used in conjunction with a reference queue (ReferenceQueue). If the object referenced by the soft reference is reclaimed by the garbage collector, the Java virtual machine will add the soft reference to the reference queue associated with it.
The solution of mass picture download and memory management in 3.android
Train of thought:
(1) download the image according to the path. After the download, save the picture in imageCache (HashTable), and download the picture locally.
(2) if you download to more than 200, if the memory of the application is insufficient (SoftReference softReference decides not to oom), android's virtual machine will start garbage collection of objects. At this time, the image obtained by SoftReference softReference will appear to be null. If it is null, you need to find the downloaded picture according to the id and path of the picture. Load and display it directly. If it has not been downloaded locally, download it directly from the network.
The code is as follows:
Public Drawable loadDrawable (final String imageUrl, final ImageCallBack imageCallback) {if (imageCache.containsKey (imageUrl)) {SoftReference softReference = imageCache.get (imageUrl); Drawable drawable = softReference.get (); if (drawable! = null) {return drawable }} final Handler handler = new Handler () {@ Override public void handleMessage (Message message) {imageCallback.imageLoaded ((Drawable) message.obj, imageUrl);}} New Thread () {@ Override public void run () {Drawable drawable = null; try {drawable = getDrawable (imageUrl); imageCache.put (imageUrl, new SoftReference (drawable));} catch (Exception e) {e.printStackTrace () } Message message = handler.obtainMessage (0, drawable); handler.sendMessage (message);}} .start (); return null;} private Drawable getDrawable (String urlString) throws Exception {if (! NetTools.isWifiConnected (context)) {getImage = DataPreference.getIsAutoGetPicture (context) } else {getImage = true;} String fileName = urlString.substring (urlString.lastIndexOf ("/") + 1). Trim (); File cacheFile = null; if (Environment.getExternalStorageState (). Equals (Environment.MEDIA_MOUNTED)) {/ / sdcard has been mounted cacheFile = new File (shopPicInSdcard + fileName) If (getImage) {if (! cacheFile.exists () & & cacheFile.createNewFile ()) {/ / cache file does not exist & & created the file and downloaded the cache image to Sdcard InputStream inputStream = getInputStreamFromHttp (urlString); writeCacheFile2SDCard (cacheFile, readStream (inputStream));} return BitmapDrawable.createFromPath (shopPicInSdcard + fileName) } else {return null;}} else {/ / get data streams from the Internet only when there is no sdcard (getImage) {InputStream inputStream = getInputStreamFromHttp (urlString); return Drawable.createFromStream (inputStream, urlString);} else {return null }
The way 4.android handles pictures in listview getView; mainly optimizes the efficiency in listview.
This is the end of the content of "what is the solution for Android to download a large number of pictures?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.