如何使用glide将图像下载到位图中?

使用Glide将URL下载到ImageView非常简单:

  Glide .with(context) .load(getIntent().getData()) .placeholder(R.drawable.ic_loading) .centerCrop() .into(imageView); 

我想知道我是否也可以下载到Bitmap中? 我想下载到一个原始的位图,然后我可以使用其他工具进行操作。 我已经通过代码,并没有看到如何做到这一点。

我对Glide不够熟悉,但是看起来如果你知道目标的大小,你可以使用像这样的东西:

 Bitmap theBitmap = Glide. with(this). load("http://...."). asBitmap(). into(100, 100). // Width and height get(); 

它看起来像你可以传递-1,-1 ,并获得一个完整的大小的图像(纯粹基于testing,不能看到它logging)。

注意into(int,int)返回一个FutureTarget<Bitmap> ,所以你必须把它包装在一个覆盖ExecutionExceptionInterruptedException的try-catch块中。 这是一个更完整的示例实现,testing和工作:

 class SomeActivity extends Activity { private Bitmap theBitmap = null; @Override protected void onCreate(Bundle savedInstanceState) { // onCreate stuff ... final ImageView image = (ImageView) findViewById(R.id.imageView); new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { Looper.prepare(); try { theBitmap = Glide. with(SomeActivity.this). load("https://www.google.eshttp://img.dovov.comsrpr/logo11w.png"). asBitmap(). into(-1,-1). get(); } catch (final ExecutionException e) { Log.e(TAG, e.getMessage()); } catch (final InterruptedException e) { Log.e(TAG, e.getMessage()); } return null; } @Override protected void onPostExecute(Void dummy) { if (null != theBitmap) { // The full bitmap should be available here image.setImageBitmap(theBitmap); Log.d(TAG, "Image loaded"); }; } }.execute(); } } 

遵循下面的注释中的Monkeyless的build议( 这似乎也是官方的方式 ),你可以使用一个SimpleTarget ,可选地与override(int,int)来大大简化代码。 但是,在这种情况下,必须提供确切的尺寸(不接受低于1的任何值):

 Glide .with(getApplicationContext()) .load("https://www.google.eshttp://img.dovov.comsrpr/logo11w.png") .asBitmap() .into(new SimpleTarget<Bitmap>(100,100) { @Override public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { image.setImageBitmap(resource); // Possibly runOnUiThread() } }); 

那么最新的Glide版本compile 'com.github.bumptech.glide:glide:4.0.0'你不必提供大小或任何东西,现在非常简单

 Glide.with(this) .asBitmap() .load(path) .into(new SimpleTarget<Bitmap>() { @Override public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) { imageView.setImageBitmap(resource); } }); 

而对于版本compile 'com.github.bumptech.glide:glide:3.7.0'和更旧版本

 Glide.with(this) .load(path) .asBitmap() .into(new SimpleTarget<Bitmap>() { @Override public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { imageView.setImageBitmap(resource); } }); 

它看起来像覆盖Target类或像BitmapImageViewTarget实现之一,并重写setResource方法来捕获位图可能是要走的路…

这是未经testing的。 🙂

  Glide.with(context) .load("http://goo.gl/h8qOq7") .asBitmap() .into(new BitmapImageViewTarget(imageView) { @Override protected void setResource(Bitmap resource) { // Do bitmap magic here super.setResource(resource); } }); 

这是我的工作: https : //github.com/bumptech/glide/wiki/Custom-targets#overriding-default-behavior

 import com.bumptech.glide.Glide; import com.bumptech.glide.request.transition.Transition; import com.bumptech.glide.request.target.BitmapImageViewTarget; ... Glide.with(yourFragment) .load("yourUrl") .asBitmap() .into(new BitmapImageViewTarget(yourImageView) { @Override public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> anim) { super.onResourceReady(bitmap, anim); Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { // Here's your generated palette Palette.Swatch swatch = palette.getDarkVibrantSwatch(); int color = palette.getDarkVibrantColor(swatch.getTitleTextColor()); } }); } });