我搜索了很多,但我不明白我的错误在哪里.
首先在我的应用程序中,如果没有网络我从网络获取图像我从创建的数据库中获取它们.
我要发布我的ImageLoader类然后是内存类然后是utils类如果有什么问题请我需要一些帮助提前谢谢.
public class ClassImageLoader {ClassMemoryCache memoryCache=new ClassMemoryCache();ClassfileCache fileCache;private Map<ImageVIEw, String> imageVIEws=Collections.synchronizedMap(new WeakHashMap<ImageVIEw, String>());ExecutorService executorService; public ClassImageLoader(Context context){ fileCache=new ClassfileCache(context); executorService=Executors.newFixedThreadPool(5);}final int stub_ID=R.drawable.restlogobutton;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); ClassUtils.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=70; 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), null, 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; } 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();}
}
public class ClassMemoryCache {private Map<String, Bitmap> cache=Collections.synchronizedMap( new linkedHashMap<String, Bitmap>(10,1.5f,true));//Last argument true for LRU orderingprivate long size=0;//current allocated sizeprivate long limit=1000000;//max memory in bytespublic ClassMemoryCache(){ //use 25% of available heap size setlimit(Runtime.getRuntime().maxMemory()/4);}public voID setlimit(long new_limit){ limit=new_limit;}public Bitmap get(String ID){ if(!cache.containsKey(ID)) return null; return cache.get(ID);}public voID put(String ID, Bitmap bitmap){ try{ if(cache.containsKey(ID)) size-=getSizeInBytes(cache.get(ID)); cache.put(ID, bitmap); size+=getSizeInBytes(bitmap); checkSize(); }catch(Throwable th){ th.printstacktrace(); }}private voID checkSize() { if(size>limit){ Iterator<Entry<String, Bitmap>> iter=cache.entrySet().iterator();//least recently accessed item will be the first one iterated while(iter.hasNext()){ Entry<String, Bitmap> entry=iter.next(); size-=getSizeInBytes(entry.getValue()); iter.remove(); if(size<=limit) break; } }}public voID clear() { cache.clear();}long getSizeInBytes(Bitmap bitmap) { if(bitmap==null) return 0; return bitmap.getRowBytes() * bitmap.getHeight();}
}
public class ClassUtils {public static voID copyStream(inputStream is, OutputStream os){ final int buffer_size=1024; try { byte[] bytes=new byte[buffer_size]; for(;;) { int count=is.read(bytes, 0, buffer_size); if(count==-1) break; os.write(bytes, 0, count); } } catch(Exception ex){}}
}
解决方法:
//在您的代码中替换此方法并尝试我不确定它是否会解决您的错误,但一旦您可以尝试.
private Bitmap getBitmap(String url) { //I IDentify images by hashcode. Not a perfect solution, good for the demo. String filename=String.valueOf(url.hashCode()); file f=new file(cacheDir, filename); //from SD cache Bitmap b = decodefile(f); if(b!=null) return b; //from web try { Bitmap bitmap=null; inputStream is=new URL(url).openStream(); 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=70; 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.inTempStorage = new byte[32*1024]; o2.inSampleSize=scale; return BitmapFactory.decodeStream(new fileinputStream(f), null, o2); } catch (fileNotFoundException e) {} return null;}
检查ImageLoaderClass的此链接:
http://code.google.com/p/vimeoid/source/browse/apk/src/com/fedorvlasov/lazylist/ImageLoader.java
OOM问题检查此链接:
Strange out of memory issue while loading an image to a Bitmap object
如果您仍然收到错误,请检查我在多次搜索后找到的这个链接并解决了我的问题:
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/vYWKY1Y6bUo
以上是内存溢出为你收集整理的android – 访问应用程序内部时出现内存缓存错误全部内容,希望文章能够帮你解决android – 访问应用程序内部时出现内存缓存错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)