2015-09-23 79 views
2

我檢索圖像名稱的字符串,並具有從服務器下載 位對於使用這個鏈接獲取圖像名稱的字符串弗朗JSON數據和下載:多線程下載圖像會導致內存不足異常

字符串str_ImgURL_bmp_pattern =「http://xxxx/xxx/MobileService.svc/DownloadFile/FileName/」+ imageName; download_PngFile(str_ImgURL_bmp_pattern);

void download_PngFile(String fileUrl) { 
     Log.e("In download_PngFile ", " str_imgList_imageaudioPath = " + imageName); 
     Bitmap imagenObtenida = null; 
     try { 
      URL ImgUrl = new URL(fileUrl); 
      HttpURLConnection conn = (HttpURLConnection) ImgUrl.openConnection(); 
      conn.connect(); 
      imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream()); 
      //Log.e("imagenObtenida", " = " + imagenObtenida); 

      String fotoname = imageName; 
      File file = new File(newFolder, fotoname); 
      int sizeOfImage = (int) file.length(); 
      Log.e("sizeOfImage ", " = " + sizeOfImage + "@ " + imageName); 
      if (file.exists()) file.delete(); 
      try { 
       FileOutputStream out = new FileOutputStream(file); 
       imagenObtenida.compress(Bitmap.CompressFormat.PNG, 100, out); 
       out.flush(); 
       out.close(); 
       // Log.e("Png = ", "DownLoad complete"); 

      } catch (Exception e) { 

      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

但是,這一些圖像下載和應用程序崩潰後,我得到的錯誤標題在logcat。

java.lang.RuntimeException: An error occured while executing doInBackground() 
      at android.os.AsyncTask$3.done(AsyncTask.java:278) 
      at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
      at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
      at java.lang.Thread.run(Thread.java:856) 
    Caused by: java.lang.OutOfMemoryError 
      at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493) 
      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:549) 
      at com.example.tazeen.classnkk.AllPosts_Page.download_PngFile(AllPosts_Page.java:896) 
      at com.example.tazeen.classnkk.AllPosts_Page.getDoenLoaddata(AllPosts_Page.java:820) 
      at com.example.tazeen.classnkk.AllPosts_Page$GetgetAllImagePath_List.doInBackground(AllPosts_Page.java:781) 
      at com.example.tazeen.classnkk.AllPosts_Page$GetgetAllImagePath_List.doInBackground(AllPosts_Page.java:771) 
      at android.os.AsyncTask$2.call(AsyncTask.java:264) 
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
            at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
            at java.lang.Thread.run(Thread.java:856) 

在此行中的錯誤:imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream());

如何解決此問題。

+0

嘗試引用此[答案] [1]。這可能會對您有所幫助。 [1]:http://stackoverflow.com/questions/14051068/java-lang-outofmemoryerror-in-android-while-getting-image-from-gallery-in-androi – Jas

+0

你加載多個圖像在同一時間? – FlanschiFox

+0

是的,同時加載多張圖片。 – androidTag

回答

1

所以基本上這個例外很明顯。 你內存不足。

有一些可能的解決方案:

1比例縮小你的圖像:

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 2; 
Bitmap imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream(), null, options); 

這是必需的,如果連一個單一的圖像下載會導致系統崩潰。

2.不要同時解碼

所以不使用ThreadPoolExecutor

3.組合 例如:

- 只是使用2個並行線程

- 檢查可用內存並設置適合當前內存的線程號

-catch內存異常,並等待其他線程完成

相關問題