Is there a way to upload a bitmap image in Glide

I need a way to use a bitmap as a contribution to Glide. I'm not even sure that this is possible. It is designed to resize. Glide has a good scaled image enhancement. The problem is that I have resources that are already loaded into memory. The only solution I could find was to save the images in a temporary file and reload them back to Glide as inputStream / file. Is there a better way to achieve this?

Please before replying. I'm not talking about exiting Glide ... .asBitmap().get() I know that I need help with input.

Here is my solution:

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

I would like to avoid writing images to the internal and uploading them again. That is why I ask if there is a way to enter as a bitmap

thanks

+20
source share
9 answers

A really strange case, but let's try to solve it. I use the old and not cool Picasso , but one day I will try Glide. Here are some links that may help you:

And actually a cruel, but I think, effective way to solve this problem:

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

I'm not sure if this will help you, but I hope this can take you one step closer to the solution.

+16
source

For version 4, you must call asBitmap() before load()

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

Additional information: http://bumptech.imtqy.com/glide/doc/targets.html

+44
source

This solution works with Glide V4. You can get a bitmap as follows:

 Bitmap bitmap = Glide .with(context) .asBitmap() .load(uri_File_String_Or_ResourceId) .submit() .get(); 

Note: this will block the current thread from loading the image.

+12
source

There are minor changes in accordance with the latest version of Glide . Now we need to use submit() to load the image as a bitmap, unless you use the submit() method, the listener will not be called.

Here is a working example that I used today.

 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(); 

This works, and I get a bitmap from the listener.

+7
source

The accepted answer works for previous versions, but in newer versions of using 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); 

Provided by

+5
source

here is another solution that returns you a bitmap for installation in your ImageView

 Glide.with(this) .load(R.drawable.card_front) // you can pass url too .asBitmap() .into(new SimpleTarget<Bitmap>() { @Override public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { // you can do something with loaded bitmap here imgView.setImageBitmap(resource); } }); 
+3
source

For what, based on the messages above, is my approach:

  Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); Uri imageUri = Uri.withAppendedPath(sArtworkUri, String.valueOf(album_id)); 

then in the adapter:

  // loading album cover using Glide library Glide.with(mContext) .asBitmap() .load(imageUri) .into(holder.thumbnail); 
0
source

In Kotlin

 Glide.with(this) .asBitmap() .load("https://...") .addListener(object : RequestListener<Bitmap> { override fun onLoadFailed( e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean ): Boolean { Toast.makeText( this@MainActivity , "failed: " + e?.printStackTrace(), Toast.LENGTH_SHORT).show() return false } override fun onResourceReady( resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean ): Boolean { //image is ready, you can get bitmap here return false } }) .into(imageView) 
0
source

Please use the implementation for this:

implementation of 'com.github.bumptech.glide: glide: 4.9.0'

  Glide.with(this) .asBitmap() .load(item.getMarkertOverlayImage()) .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) { } }); 
0
source

Source: https://habr.com/ru/post/1264304/


All Articles