2016-07-30 390 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传递给成员函数)。

相关问题