Reputation: 85
I want to load my image into bitmap using glide but it raise the error
java.lang.IllegalArgumentException: You must call this method on a background thread at com.bumptech.glide.util.Util.assertBackgroundThread(Util.java:141) at com.bumptech.glide.request.RequestFutureTarget.doGet(RequestFutureTarget.java:186) at com.bumptech.glide.request.RequestFutureTarget.get(RequestFutureTarget.java:108) at extend.BitmapLoader.loadBitmapFromFile_Glide(BitmapLoader.java:60)
this is my code :
_runnable_animation = new Runnable() {
@Override
public void run() {
bm = get_bitmap();
}
};
_handler_animation.postDelayed(_runnable_animation, _RUNNABLE_THREAD_DELAY);
and this is my get_bitmap() method :
try {
return Glide.with(context)
.asBitmap()
.load(new File(path))
.submit(500, 500)
.get();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} catch (ExecutionException e) {
e.printStackTrace();
return null;
}
Upvotes: 1
Views: 1532
Reputation: 27515
I'm using below method for getting bitmap
public static void loadBitmapFromServer(Context context, String url, final OnBitmapLoadedListener callback) {
try {
Glide.with(context).load(url)
.asBitmap()
.priority(Priority.IMMEDIATE)
.listener(new RequestListener<String, Bitmap>() {
@Override
public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
// you can do whatever you want to do with Bitamp
callback.onBitmapLoaded(resource);
return false;
}
});
} catch (Exception e) {}
}
Declare interface like
public interface OnBitmapLoadedListener {
void onBitmapLoaded(Bitmap resource);
}
Upvotes: 1
Reputation: 845
You should define the bitmap variable globally, like this:
Bitmap bitmap;
Glide.with(context).load(url)
.asBitmap()
.listener(new RequestListener<String, Bitmap>() {
@Override
public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
// After getting bitmap
bitmap = resource
return false;
}
});
Upvotes: 0
Reputation: 2877
@NJ answer was good enough but still if you want your method to get that work, create background thread handler instead main thread handler. here I have modified your code to make that work.
HandlerThread bgThread = new HandlerThread("BitmapLoaderThread");
bgThread.start();
// initialize your _handler_animation like below
Handler _handler_animation = new Handler(bgThread.getLooper());
Runnable _runnable_animation = new Runnable() {
@Override
public void run() {
bm = get_bitmap();
}
};
_handler_animation.postDelayed(_runnable_animation, _RUNNABLE_THREAD_DELAY);
Do cleanup as well, better place is Activity#onDestroy
@Override
public void onDestroy() {
_handler_animation.getLooper().quit();
super.onDestroy();
}
Upvotes: 1
Reputation: 946
Make sure that you started new thread like below
mThread = new Thread(_runnable_animation);
mThread.start();
Upvotes: 0