2015-10-13 60 views
-1

在這裏,我通過dalvikvm堆的一個.Getting錯誤下載從服務器的一個圖像:在31961104字節分配時內存運行應用程序,並在中途墜毀。一段時間圖像下載某個not.But我想從下載列表視圖中添加圖像..安卓:dalvikvm堆:內存

這裏是我的下載代碼

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

的完整鏈接的android:largeHeap = 「真」 做u設置此清單文件? –

+0

爲什麼不使用圖像加載庫在列表視圖中添加圖像? –

+1

加入'android:largerHeap =「true」'不是管理這個問題的正確方法。 –

回答

0

這個問題,當你正在下載圖像,並沒有留下記憶這是下載一些圖片後,分配給您的應用程序出現。有各種不同的圖像加載庫,使您的工作更容易:FrescoPicassoGlide

  1. 相比,我建議你通過Facebook的是Fresco是相當真棒和成熟結賬圖像管理庫到其他圖像加載庫。

  2. 壁畫具有SimpleDraweeView作爲支持Rounded Corners and Circleslink並支持動畫自定義圖像視圖(.GIF,.WEBP)以及正常圖像(.JPG,.PNG)。

  3. 壁畫處理3層架構(BITMAP_MEMORY_CACHE,ENCODED_MEMORY_CACHEDISK_CACHE)緩存圖像的所有內容。它也減少了OOM(內存不足)問題。當視圖中的圖像超出屏幕時,它會自動回收位圖,從而釋放內存。

您可以通過在清單中添加android:largeHeap="true"來增加分配給應用程序的內存。

注:增加heap爲您的應用程序不被認爲是一個理想的解決方案。

下面是來自谷歌的提取物來解釋它,

然而,要求一大堆的能力僅用於一 小集合的應用程序,可以證明需要消耗更多的RAM(例如 作爲一個大型照片編輯應用程序)。千萬不要簡單地請求大堆,因爲內存不足,需要快速修復 - 只有當您確切知道分配了所有內存的位置以及必須保留的原因時,您才應該使用 。但是,即使您對自己的應用程序有足夠的信心,也可以在任何可能的範圍內儘可能避免向 發送請求。使用額外的內存將日益成爲 到整體用戶體驗的損害,因爲垃圾收集 將需要更長的時間和系統性能可能會較慢時 任務切換或執行其他常見操作。

這裏的文檔https://developer.android.com/training/articles/memory.html