2016-07-30 389 views
1

我使用glide將其資源ID加載到位圖中,並且我想將位圖傳遞給成員函數一個自定義視圖類。Glide錯誤:java.lang.IllegalArgumentException - 您必須在後臺線程上調用此方法

這是我的代碼:

try { 
     Bitmap bm=Glide. 
       with(getApplicationContext()). 
       load(mThumbIds[position]). 
       asBitmap(). 
       into(width, height). // Width and height 
       get(); 
     drawView.setImg(bm); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } 

每當我嘗試運行它,我得到以下錯誤:

Unable to start activity ComponentInfo{...}: java.lang.IllegalArgumentException: YOu must call this method on a background thread

我該如何解決這個問題?

+0

可能的重複[如何使用glide將圖像下載到位圖?](http://stackoverflow.com/questions/27394016/how-does-one-use-glide-to-download-an-圖像 - 進入 - 一個位圖) –

回答

0

您不能在主線程上調用get(),因爲它執行長時間運行的任務並可能導致性能問題。

取而代之,考慮將子類別ImageViewTargetViewTarget和使用into()而不是get()into()將異步加載圖像,您可以使用自定義子類調用onReasourceReady中的相應成員函數。

當調用onLoadFailedonLoadCleared時,一定要清除您的位圖(通常將null傳遞給成員函數)。

相關問題