我想使用滑行将图像加载到位图中,但会引发错误


  java.lang.IllegalArgumentException:您必须在后台线程上调用此方法
         在com.bumptech.glide.util.Util.assertBackgroundThread(Util.java:141)
         在com.bumptech.glide.request.RequestFutureTarget.doGet(RequestFutureTarget.java:186)
         在com.bumptech.glide.request.RequestFutureTarget.get(RequestFutureTarget.java:108)
         在extend.BitmapLoader.loadBitmapFromFile_Glide(BitmapLoader.java:60)


这是我的代码:

_runnable_animation = new Runnable() {
            @Override
            public void run() {
                bm = get_bitmap();
            }
};

_handler_animation.postDelayed(_runnable_animation, _RUNNABLE_THREAD_DELAY);


这是我的get_bitmap()方法:

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;
        }

最佳答案

我正在使用以下方法获取位图

 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) {}
        }


声明界面如

 public interface OnBitmapLoadedListener {
        void onBitmapLoaded(Bitmap resource);
    }

10-08 06:14
查看更多