search

Home  >  Q&A  >  body text

android如何同步得到bitmap

android通过for循环得到bitmap,不知道是不是异步的原因,uri每次都一样,如何解决,如何同步得到bitmap,我的uri也可能是网页url或者本地地址

      for (ImagesEntity imagesEntity : imagesEntities) {
                    final String uri = imagesEntity.getImage();
                BitmapUtils.getBitmap(getActivity(), uri, new BitmapUtils.LoadImageCallback() {
                    @Override
                    public void callback(Bitmap result) {
//getBitmap
                    }
                });
            }
  public static void getBitmap(Context context, String uri, final LoadImageCallback callback) {
        LogUtils.d("getBitmap: " + uri);
        Glide.with(context)
                .load(uri)
                .asBitmap()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                        callback.callback(bitmap);
                    }
                });
    }
PHPzPHPz2771 days ago482

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:26:41

    It can be placed in a thread. You can access the bitmap in this way and write the following method in the loop

     bitmap = Glide.with(context)
                    .load(getGlideUrl(url))
                    .asBitmap()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .get();

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:26:41

    uri is read from imagesEntity, uri is the same every time and has nothing to do with synchronization and asynchronousness
    In addition, if you want to change this asynchronous callback to synchronous, just add get() after it, this method will wait until the result is returned

    reply
    0
  • Cancelreply