android – Imageloader类在库中创建lazylist文件夹.如何避免它

android – Imageloader类在库中创建lazylist文件夹.如何避免它,第1张

概述我正在使用imageLoader类从url载入图像.但是这些图像都是通过名为LazyList的文件夹名存储在图库中.它需要高达40-100 mb的内存.但是我不想加载该图像,因为用户可能会感到不适.对不起,英文不好 一切工作正常,但它在画廊中创建一个文件夹,并显示我的应用程序正在使用的所有图像.所以我觉得用户会感到不舒服的我的应用程序. 这是我的图像加载器的代码,并且它连接几个其他类甚至 publ 我正在使用imageLoader类从url载入图像.但是这些图像都是通过名为LazyList的文件夹名存储在图库中.它需要高达40-100 mb的内存.但是我不想加载该图像,因为用户可能会感到不适.对不起,英文不好

一切工作正常,但它在画廊中创建一个文件夹,并显示我的应用程序正在使用的所有图像.所以我觉得用户会感到不舒服的我的应用程序.

这是我的图像加载器的代码,并且它连接几个其他类甚至

public class ImageLoader {MemoryCache memoryCache = new MemoryCache();fileCache fileCache;private Map<ImageVIEw,String> imageVIEws = Collections        .synchronizedMap(new WeakHashMap<ImageVIEw,String>());ExecutorService executorService;public ImageLoader(Context context) {    fileCache = new fileCache(context);    executorService = Executors.newFixedThreadPool(5);}final int stub_ID = R.drawable.abs__ab_bottom_transparent_light_holo;public voID displayImage(String url,ImageVIEw imageVIEw) {    imageVIEws.put(imageVIEw,url);    Bitmap bitmap = memoryCache.get(url);    if (bitmap != null)        imageVIEw.setimageBitmap(bitmap);    else {        queuePhoto(url,imageVIEw);        imageVIEw.setimageResource(stub_ID);    }}private voID queuePhoto(String url,ImageVIEw imageVIEw) {    PhotoToload p = new PhotoToload(url,imageVIEw);    executorService.submit(new Photosloader(p));}private Bitmap getBitmap(String url) {    file f = fileCache.getfile(url);    // from SD cache    Bitmap b = decodefile(f);    if (b != null)        return b;    // from web    try {        Bitmap bitmap = null;        URL imageUrl = new URL(url);        httpURLConnection conn = (httpURLConnection) imageUrl                .openConnection();        conn.setConnectTimeout(30000);        conn.setReadTimeout(30000);        conn.setInstanceFollowRedirects(true);        inputStream is = conn.getinputStream();        OutputStream os = new fileOutputStream(f);        Utils.copyStream(is,os);        os.close();        bitmap = decodefile(f);        return bitmap;    } catch (Exception ex) {        ex.printstacktrace();        return null;    }}// decodes image and scales it to reduce memory consumptionprivate Bitmap decodefile(file f) {    try {        // decode image size        BitmapFactory.Options o = new BitmapFactory.Options();        o.inJustDecodeBounds = true;        BitmapFactory.decodeStream(new fileinputStream(f),null,o);        // Find the correct scale value. It should be the power of 2.        final int required_SIZE = 100;        int wIDth_tmp = o.outWIDth,height_tmp = o.outHeight;        int scale = 1;        while (true) {            if (wIDth_tmp / 2 < required_SIZE                    || height_tmp / 2 < required_SIZE)                break;            wIDth_tmp /= 2;            height_tmp /= 2;            scale *= 2;        }        // decode with inSampleSize        BitmapFactory.Options o2 = new BitmapFactory.Options();        o2.inSampleSize = scale;        return BitmapFactory.decodeStream(new fileinputStream(f),o2);    } catch (fileNotFoundException e) {    }    return null;}// Task for the queueprivate class PhotoToload {    public String url;    public ImageVIEw imageVIEw;    public PhotoToload(String u,ImageVIEw i) {        url = u;        imageVIEw = i;    }}class Photosloader implements Runnable {    PhotoToload photoToload;    Photosloader(PhotoToload photoToload) {        this.photoToload = photoToload;    }    @OverrIDe    public voID run() {        if (imageVIEwReused(photoToload))            return;        Bitmap bmp = getBitmap(photoToload.url);        memoryCache.put(photoToload.url,bmp);        if (imageVIEwReused(photoToload))            return;        Bitmapdisplayer bd = new Bitmapdisplayer(bmp,photoToload);        Activity a = (Activity) photoToload.imageVIEw.getContext();        a.runOnUiThread(bd);    }}boolean imageVIEwReused(PhotoToload photoToload) {    String tag = imageVIEws.get(photoToload.imageVIEw);    if (tag == null || !tag.equals(photoToload.url))        return true;    return false;}// Used to display bitmap in the UI threadclass Bitmapdisplayer implements Runnable {    Bitmap bitmap;    PhotoToload photoToload;    public Bitmapdisplayer(Bitmap b,PhotoToload p) {        bitmap = b;        photoToload = p;    }    public voID run() {        if (imageVIEwReused(photoToload))            return;        if (bitmap != null)            photoToload.imageVIEw.setimageBitmap(bitmap);        else            photoToload.imageVIEw.setimageResource(stub_ID);    }}public voID clearCache() {    memoryCache.clear();    fileCache.clear();}}
解决方法 我通过更改我的fileCache类及其目标文件夹得到我的解决方案
public class fileCache {    private file cacheDir;    private file nomediafile;    String NOMEDIA = " .nomedia";public fileCache(Context context) {    // Find the dir to save cached images    if (androID.os.Environment.getExternalStorageState().equals(            androID.os.Environment.MEDIA_MOUNTED)) {        cacheDir = new file(Environment.getExternalStorageDirectory()                + "/mydir");        if (cacheDir.mkdir()) {            nomediafile = new file(                    Environment.getExternalStorageDirectory() + "/mydir/"                            + NOMEDIA);            if (!nomediafile.exists()) {                try {                    nomediafile.createNewfile();                } catch (IOException e) {                    // Todo auto-generated catch block                    e.printstacktrace();                }            }        }    } else {        cacheDir = context.getCacheDir();    }    if (!cacheDir.exists())        cacheDir.mkdirs();}public file getfile(String url) {    // I IDentify images by hashcode. Not a perfect solution,good for the    // demo.    // String filename=String.valueOf(url.hashCode());    // Another possible solution (thanks to grantland)    @SuppressWarnings("deprecation")    String filename = URLEncoder.encode(url);    file f = new file(cacheDir,filename);    return f;}public voID clear() {    file[] files = cacheDir.Listfiles();    if (files == null)        return;    for (file f : files)        f.delete();}}
总结

以上是内存溢出为你收集整理的android – Imageloader类在库中创建lazylist文件夹.如何避免它全部内容,希望文章能够帮你解决android – Imageloader类在库中创建lazylist文件夹.如何避免它所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1132266.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存