这里我正逐一从服务器下载图片。获取dalvikvm-heap的错误:在运行应用程序时在31961104字节分配内存不足并在中间崩溃。有时候某些时间图像下载没有。但是我想从下载中添加Listview中的图像..
这是我的下载代码
private Bitmap downloadBitmap(String url) {
final DefaultHttpClient client = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();
// decoding stream data back into image Bitmap that android understands
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
// final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
File file = new File(newFolder, imageName);
if (file.exists()) file.delete();
try
{
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
int imagenObtenidaW = bitmap.getWidth();
int imagenObtenidaH = bitmap.getHeight();
Log.e("imagenObtenidaW " ," = +" + imagenObtenidaW + " imagenObtenidaH = " + imagenObtenidaH);
} catch (Exception e) {
}
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// You Could provide a more explicit error message for IOException
getRequest.abort();
Log.e("ImageDownloader", "Something went wrong while" +
" retrieving bitmap from " + url + e.toString());
}
return null;
}
here is my code form download in the same Activity.
if (imageName.endsWith(png_Pattern) || imageName.endsWith(jpg_pattern) || imageName.endsWith(bmp_pattern) || imageName.endsWith(gif_pattern) || imageName.endsWith(jpeg_pattern))
{
str_DownLoadUrl = namespace + "/DownloadFile/FileName/" + imageName;
Log.e("@@@@@", "str_DownLoadUrl = " + str_DownLoadUrl);
//download_PngFile(str_DownLoadUrl);
downloadBitmap(str_DownLoadUrl);
strDownLoadStatus = "1";
dbhelper.update_DownLoadStatus(imageName, strDownLoadStatus);
}
}
这是我的logcat
1.414 10598-10601/com.example.tazeen.classnkk D/dalvikvm﹕ GC_CONCURRENT freed 1738K, 57% free 15379K/35335K, paused 5ms+7ms
10-13 14:52:11.963 10598-10826/com.example.tazeen.classnkk D/dalvikvm﹕ GC_FOR_ALLOC freed 64K, 57% free 15377K/35335K, paused 95ms
10-13 14:52:11.963 10598-10826/com.example.tazeen.classnkk I/dalvikvm-heap﹕ Forcing collection of SoftReferences for 31961104-byte allocation
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk D/dalvikvm﹕ GC_BEFORE_OOM freed 9K, 57% free 15368K/35335K, paused 68ms
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk E/dalvikvm-heap﹕ Out of memory on a 31961104-byte allocation.
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ "AsyncTask #2" prio=5 tid=13 RUNNABLE
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ | group="main" sCount=0 dsCount=0 obj=0x42ad4188 self=0x1edbd8
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ | sysTid=10826 nice=10 sched=0/0 cgrp=bg_non_interactive handle=1591808
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ | schedstat=( 12449561867 3389288161 878 ) utm=1181 stm=63 core=0
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at com.example.tazeen.classnkk.AllPosts_Page.downloadBitmap(AllPosts_Page.java:1220)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at com.example.tazeen.classnkk.AllPosts_Page.getDoenLoaddata(AllPosts_Page.java:738)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at com.example.tazeen.classnkk.AllPosts_Page$GetgetDoenLoaddata.doInBackground(AllPosts_Page.java:700)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at com.example.tazeen.classnkk.AllPosts_Page$GetgetDoenLoaddata.doInBackground(AllPosts_Page.java:696)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ at java.lang.Thread.run(Thread.java:856)
10-13 14:52:12.034 10598-10826/com.example.tazeen.classnkk I/dalvikvm﹕ [ 10-13 14:52:12.034 10598:0x2a4a D/skia ]
--- decoder->decode returned false
如何解决这个问题。
答案 0 :(得分:0)
当您下载图像时,会出现此问题,并且在下载某些图像后没有剩余的内存分配给您的应用程序。有各种图像加载库可以让您的工作更轻松:Fresco,Picasso,Glide等。
我建议您查看图片管理库(Facebook),Fresco与其他图片加载库相比非常棒且成熟。
< / LI> Fresco拥有SimpleDraweeView
作为自定义图片视图,支持Rounded Corners and Circles
link并支持动画(.gif,.webp)以及普通图像(.jpg,.png) )。
Fresco使用3层架构(BITMAP_MEMORY_CACHE
,ENCODED_MEMORY_CACHE
和DISK_CACHE
)处理图像的所有缓存。它还减少了OOM(Out Of Memory)问题。当视图中的图像离开屏幕时,它会自动回收位图,从而释放内存。
您可以通过在清单中添加android:largeHeap="true"
来增加分配给您的应用程序的内存。
注意:为您的应用程序增加heap
并不被视为理想的解决方案。
以下是Google解压缩的摘录,
但是,请求大堆的功能仅适用于a 一小组应用程序,可以证明需要消耗更多的RAM(例如 作为一个大型照片编辑应用程序)。永远不要简单地请求大堆 因为你的内存耗尽,需要快速修复 - 你应该这样做 只有当你确切知道所有记忆的存在时才使用它 分配以及必须保留的原因。然而,即使你有信心 你的应用程序可以证明大堆的合理性,你应该避免请求它 尽可能的。使用额外的内存将越来越多 因为垃圾而损害整体用户体验 收集将花费更长时间,系统性能可能会更慢 任务切换或执行其他常见操作。
这是文档https://developer.android.com/training/articles/memory.html
的完整链接