Java-based code for capturing and saving images in the Wemall app

Source: Internet
Author: User

Wemall-mobile is based on the Wemall Android App Store, only need to upload the interface file in the original mall directory to complete the server configuration, the client can be customized modification. Share a collection of images and images to save the download three ways to get managers, network URL acquisition, memory cache acquisition, external file cache access code for the technician to learn the reference to use.

Package Com.inuoer.util;import Java.lang.ref.softreference;import Java.util.hashmap;import java.util.HashSet; Import Java.util.map;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Android.content.context;import Android.graphics.bitmap;import Android.os.handler;public class AsyncImageLoader {// Save the collection of picture URLs that are being downloaded, avoid duplicate downloads with private static hashset<string> sdownloadingset;//soft reference memory cache private static map<string, Softreference<bitmap>> simagecache;//Pictures Three ways to get manager, network URL acquisition, memory cache acquisition, external file cache get private static Loaderimpl impl;// Thread pool-related private static executorservice sexecutorservice;//notification UI thread picture gets OK when using private Handler handler;/** * Asynchronously loads the finished callback interface */ Public interface Imagecallback {/** * callback function * * @param bitmap *: May be null! * @param imageUrl */public void onimageloaded (Bitmap Bitmap, String imageUrl);} static {Sdownloadingset = new hashset<string> (); simagecache = new hashmap<string, softreference<bitmap> > (); impl = new Loaderimpl (SIMagecache);} Public Asyncimageloader (Context context) {handler = new handler (); Startthreadpoolifnecessary (); String DefaultDir = Context.getcachedir (). GetAbsolutePath (); Setcacheddir (DefaultDir);} /** * Whether to cache images to/data/data/package/cache/directory by default does not cache */public void Setcache2file (Boolean flag) {impl.setcache2file (flag);} /** * Set Cache path, Setcache2file (TRUE) when valid */public void Setcacheddir (String dir) {impl.setcacheddir (dir);} /** open thread pool */public static void Startthreadpoolifnecessary () {if (Sexecutorservice = = NULL | | Sexecutorservice.isshutdown ( )|| Sexecutorservice.isterminated ()) {Sexecutorservice = Executors.newfixedthreadpool (3);//Sexecutorservice = Executors.newsinglethreadexecutor ();}} /** * Asynchronously download the picture and cache it into memory * * @param URL * @param callback * See Imagecallback interface */public void download Image (final String URL, final Imagecallback callback) {downloadimage (URL, True, callback);} /** * * @param URL * @param cache2memory * is cached in memory * @param callback */public void downloadImage (final String URL, final boolean cache2memory,final Imagecallback callback) {if (Sdownloadingset.contains (URL)) {// LOG.I ("Asyncimageloader", "# # #该图片正在下载, cannot be downloaded repeatedly! "); return;} Bitmap Bitmap = impl.getbitmapfrommemory (URL), if (Bitmap! = null) {if (callback! = null) {callback.onimageloaded (Bitmap, U RL);}} else {//download picture Sdownloadingset.add (URL) from network side; Sexecutorservice.submit (new Runnable () {@Overridepublic void Run () {final Bitmap Bitmap = Impl.getbitmapfromurl (url,cache2memory); Handler.post (new Runnable () {@Overridepublic void run () {if ( Callback! = null) callback.onimageloaded (bitmap, URL); sdownloadingset.remove (URL);}});}});} /** * Preload next image, cache to memory * * @param url */public void preloadnextimage (final String URL) {//Set callback to empty, only bitmap cached to Mem Ory can be. Downloadimage (URL, null);}}

  

Wemall-mobile Details:http://www.koahub.com/home/product/56

Wemall website Address: http://www.wemallshop.com

Wemall Open Source Micro-mall, mall, mall source code, level three distribution, micro-fresh, micro-fruit, micro-takeaway, micro-ordering---professional system

Java-based code for capturing and saving images in the Wemall app

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.