Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to load image as bitmap to Glide

Im looking for a way to use bitmap as input to Glide. I am even not sure if its possible. It's for resizing purposes. Glide has a good image enhancement with scale. The problem is that I have resources as bitmap already loaded to memory. The only solution I could find is to store images to temporary file and reload them back to Glide as inputStream/file.. Is there a better way to achieve that ?

Please before answering .. Im not talking about output from Glide.. .asBitmap().get() I know that.I need help with input.

Here is my workaround 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'd like to avoid writing images to internal and load them again.That is the reason why Im asking if there is way to to have input as bitmap

Thanks

like image 300
Maher Abuthraa Avatar asked Feb 16 '17 15:02

Maher Abuthraa


People also ask

How do you get pictures off of Glide?

To simply load an image to LinearLayout, we call the with() method of Glide class and pass the context, then we call the load() method, which contains the URL of the image to be downloaded and finally we call the into() method to display the downloaded image on our ImageView.

How do I change the default picture in Glide Android?

Add a template column in the Glide data editor, containing the image or url of the default picture. Then, add an if/then/else column in the data editor : if “user image” is empty then “default image” else “user image”.


2 Answers

For version 4 you have to 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) {}             });         } 

More info: http://bumptech.github.io/glide/doc/targets.html

like image 55
Teffi Avatar answered Sep 21 '22 08:09

Teffi


This solution is working with Glide V4. You can get the bitmap like this:

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

Note: this will block the current thread to load the image.

like image 32
Tom Sabel Avatar answered Sep 20 '22 08:09

Tom Sabel