欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

在Android上使用ContentProvider搜索手机照片,模拟微信展示本地图片功能

最编程 2024-01-25 11:18:15
...
  • package com.example.imagescan;  
  •   
  • import java.util.concurrent.ExecutorService;  
  • import java.util.concurrent.Executors;  
  •   
  • import android.graphics.Bitmap;  
  • import android.graphics.BitmapFactory;  
  • import android.graphics.Point;  
  • import android.os.Handler;  
  • import android.os.Message;  
  • import android.support.v4.util.LruCache;  
  •   
  • /** 
  •  * 本地图片加载器,采用的是异步解析本地图片,单例模式利用getInstance()获取NativeImageLoader实例 
  •  * 调用loadNativeImage()方法加载本地图片,此类可作为一个加载本地图片的工具类 
  •  *  
  •  * @blog http://blog.****.net/xiaanming 
  •  *  
  •  * @author xiaanming 
  •  * 
  •  */  
  • public class NativeImageLoader {  
  •     private LruCache<String, Bitmap> mMemoryCache;  
  •     private static NativeImageLoader mInstance = new NativeImageLoader();  
  •     private ExecutorService mImageThreadPool = Executors.newFixedThreadPool(1);  
  •       
  •       
  •     private NativeImageLoader(){  
  •         //获取应用程序的最大内存  
  •         final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);  
  •   
  •         //用最大内存的1/4来存储图片  
  •         final int cacheSize = maxMemory / 4;  
  •         mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {  
  •               
  •             //获取每张图片的大小  
  •             @Override  
  •             protected int sizeOf(String key, Bitmap bitmap) {  
  •                 return bitmap.getRowBytes() * bitmap.getHeight() / 1024;  
  •             }  
  •         };  
  •     }  
  •       
  •     /** 
  •      * 通过此方法来获取NativeImageLoader的实例 
  •      * @return 
  •      */  
  •     public static NativeImageLoader getInstance(){  
  •         return mInstance;  
  •     }  
  •       
  •       
  •     /** 
  •      * 加载本地图片,对图片不进行裁剪 
  •      * @param path 
  •      * @param mCallBack 
  •      * @return 
  •      */  
  •     public Bitmap loadNativeImage(final String path, final NativeImageCallBack mCallBack){  
  •         return this.loadNativeImage(path, null, mCallBack);  
  •     }  
  •       
  •     /** 
  •      * 此方法来加载本地图片,这里的mPoint是用来封装ImageView的宽和高,我们会根据ImageView控件的大小来裁剪Bitmap 
  •      * 如果你不想裁剪图片,调用loadNativeImage(final String path, final NativeImageCallBack mCallBack)来加载 
  •      * @param path 
  •      * @param mPoint 
  •      * @param mCallBack 
  •      * @return 
  •      */  
  •     public Bitmap loadNativeImage(final String path, final Point mPoint, final NativeImageCallBack mCallBack){  
  •         //先获取内存中的Bitmap  
  •         Bitmap bitmap = getBitmapFromMemCache(path);  
  •           
  •         final Handler mHander = new Handler(){  
  •   
  •             @Override  
  •             public void handleMessage(Message msg) {  
  •                 super.handleMessage(msg);  
  •                 mCallBack.onImageLoader((Bitmap)msg.obj, path);  
  •             }  
  •               
  •         };  
  •           
  •         //若该Bitmap不在内存缓存中,则启用线程去加载本地的图片,并将Bitmap加入到mMemoryCache中  
  •         if(bitmap == null){  
  •             mImageThreadPool.execute(new Runnable() {  
  •                   
  •                 @Override  
  •                 public void run() {  
  •                     //先获取图片的缩略图  
  •                     Bitmap mBitmap = decodeThumbBitmapForFile(path, mPoint == null ? 0: mPoint.x, mPoint == null ? 0: mPoint.y);  
  •                     Message msg = mHander.obtainMessage();  
  •                     msg.obj = mBitmap;  
  •                     mHander.sendMessage(msg);  
  •                       
  •                     //将图片加入到内存缓存  
  •                     addBitmapToMemoryCache(path, mBitmap);  
  •                 }  
  •             });  
  •         }  
  •         return bitmap;  
  •           
  •     }  
  •   
  •       
  •       
  •     /** 
  •      * 往内存缓存中添加Bitmap 
  •      *  
  •      * @param key 
  •      * @param bitmap 
  •      */  
  •     private void addBitmapToMemoryCache(String key, Bitmap bitmap) {  
  •         if (getBitmapFromMemCache(key) == null && bitmap != null) {  
  •             mMemoryCache.put(key, bitmap);  
  •         }  
  •     }  
  •   
  •     /** 
  •      * 根据key来获取内存中的图片 
  •      * @param key 
  •      * @return 
  •      */  
  •     private Bitmap getBitmapFromMemCache(String key) {  
  • 上一篇: 使用Python实现感知机算法—— HanLP教程

    下一篇: 快速找出Android SD卡中的文件,使用实用正则表达式扫描