有没有办法将图像作为位图加载到Glide

Mah*_*raa 28 android image-resizing image-scaling android-glide

我正在寻找一种方法来使用位图作为Glide的输入.我甚至不确定它是否可能.这是为了调整大小的目的.Glide具有良好的图像增强功能.问题是我有资源作为位图已经加载到内存.我能找到的唯一解决方案是将图像存储到临时文件中,并将它们作为inputStream/file重新加载回Glide ..有没有更好的方法来实现?

请在回答之前..我不是在谈论Glide的输出.. .asBitmap().get()我知道.我需要帮助输入.

这是我的解决方案:

 Bitmap bitmapNew=null;
        try {
            //
            ContextWrapper cw = new ContextWrapper(ctx);
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
            File file=new File(directory,"temp.jpg");
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.close();
            //
            bitmapNew = Glide
                    .with(ctx)
                    .load(file)
                    .asBitmap()
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(true)
                    .into( mActualWidth, mActualHeight - heightText)
                    .get();

            file.delete();
        } catch (Exception e) {
            Logcat.e( "File not found: " + e.getMessage());
        }
Run Code Online (Sandbox Code Playgroud)

我想避免将图像写入内部并再次加载它们.这就是为什么我要问是否有办法将输入作为位图

谢谢

Tef*_*ffi 57

对于4版本你必须调用asBitmap()之前load()

GlideApp.with(itemView.getContext())
        .asBitmap()
        .load(data.getImageUrl())
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {}
            });
        }
Run Code Online (Sandbox Code Playgroud)

更多信息:http://bumptech.github.io/glide/doc/targets.html

  • 哦滑翔,为什么要改变它并让人们在几个小时内搜索错误:/ (10认同)

Yur*_*sap 16

一个非常奇怪的案例,但我们试着解决它.我正在使用旧的而不是很酷Picasso,但有一天我会试试Glide.以下是一些可以帮助您的链接:

实际上,这是一种残酷的但我认为有效的解决方法:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
  yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
  Glide.with(this)
      .load(stream.toByteArray())
      .asBitmap()
      .error(R.drawable.ic_thumb_placeholder)
      .transform(new CircleTransform(this))
      .into(imageview);
Run Code Online (Sandbox Code Playgroud)

我不确定这是否会对您有所帮助,但我希望它可以让您更接近解决方案.


Exa*_*aqt 15

此解决方案适用于Glide V4.您可以像这样获取位图:

Bitmap bitmap = Glide
    .with(context)
    .asBitmap()
    .load(uri_File_String_Or_ResourceId)
    .submit()
    .get();
Run Code Online (Sandbox Code Playgroud)

注意:这将阻止当前线程加载图像.


Ash*_*mar 7

根据最新版本,几乎没有变化Glide.现在我们需要使用submit()加载图像作为位图,如果你没有类,那么submit()将不会调用监听器.

这是我今天使用的工作示例.

Glide.with(cxt)
  .asBitmap().load(imageUrl)
  .listener(new RequestListener<Bitmap>() {
      @Override
      public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Bitmap> target, boolean b) {
          Toast.makeText(cxt,getResources().getString(R.string.unexpected_error_occurred_try_again),Toast.LENGTH_SHORT).show();
          return false;
      }

      @Override
      public boolean onResourceReady(Bitmap bitmap, Object o, Target<Bitmap> target, DataSource dataSource, boolean b) {
          zoomImage.setImage(ImageSource.bitmap(bitmap));
          return false;
      }
  }
).submit();
Run Code Online (Sandbox Code Playgroud)

它正在工作,我从侦听器获取位图.


Ars*_*ool 6

请为此使用实现:

实现 'com.github.bumptech.glide:glide:4.9.0'

     Glide.with(this)
     .asBitmap()
      .load("http://url")
    .into(new CustomTarget <Bitmap>() {   
@Override  
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition <? super Bitmap> transition) { 
                // you can do something with loaded bitmap here

 }
@Override 
public void onLoadCleared(@Nullable Drawable placeholder) { 
 } 
});
Run Code Online (Sandbox Code Playgroud)


May*_*ara 6

Glide 的大部分 API 和方法现在都已弃用。以下适用于 Glide 4.9 和 Android 10。

对于图像 URI

  Bitmap bitmap = Glide
    .with(context)
    .asBitmap()
    .load(image_uri_or_drawable_resource_or_file_path)
    .submit()
    .get();
Run Code Online (Sandbox Code Playgroud)

在 build.gradle 中使用 Glide 如下

implementation 'com.github.bumptech.glide:glide:4.9.0'
Run Code Online (Sandbox Code Playgroud)

  • 此方法返回错误以将其运行到后台线程。 (3认同)

Saf*_*eer 5

接受的答案适用于以前的版本,但在新版本的Glide中使用:

RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(android.R.drawable.waiting);
requestOptions.error(R.drawable.waiting);
Glide.with(getActivity()).apply(requestOptions).load(imageUrl).into(imageView);
Run Code Online (Sandbox Code Playgroud)

礼貌