How to Load Image as Bitmap to Glide

How does one use glide to download an image into a bitmap?

Make sure you are on the Lastest version

implementation 'com.github.bumptech.glide:glide:4.10.0'

Kotlin:

Glide.with(this)
.asBitmap()
.load(imagePath)
.into(object : CustomTarget<Bitmap>(){
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
imageView.setImageBitmap(resource)
}
override fun onLoadCleared(placeholder: Drawable?) {
// this is called when imageView is cleared on lifecycle call or for
// some other reason.
// if you are referencing the bitmap somewhere else too other than this imageView
// clear it here as you can no longer have the bitmap
}
})

Bitmap Size:

if you want to use the original size of the image use the default constructor as above, else You can pass your desired size for bitmap

into(object : CustomTarget<Bitmap>(1980, 1080)

Java:

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

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});

Old Answer:

With compile 'com.github.bumptech.glide:glide:4.8.0' and below

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

For compile 'com.github.bumptech.glide:glide:3.7.0' and below

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

Now you might see a warning SimpleTarget is deprecated

Reason:

The main point of deprecating SimpleTarget is to warn you about the
ways in which it tempts you to break Glide's API contract.
Specifically, it doesn't do anything to force you to stop using any
resource you've loaded once the SimpleTarget is cleared, which can
lead to crashes and graphical corruption.

The SimpleTarget still can be used as long you make sure you are not using the bitmap once the imageView is cleared.

Is there a way to load image as bitmap to Glide

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

Glide image from gallery to bitmap variable

I don't know if you should even think about doing this in some serious project but for now it did what needs to be done for me. Earlier I declared private Bitmap jopa.

Glide.with(getContext()).asBitmap().
load(selectedImage).override(500,500).
into(new CustomTarget<Bitmap>()
{
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
imgShare.setImageBitmap(resource);
jopa = ((BitmapDrawable)imgShare.getDrawable()).getBitmap();
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {

}
});

Now Bitmap jopa can be used for other tasks. I hope. Tested it to change picture of another imageview, it worked, so I'll pray it will work further. Full onActivityResult code here. Don't want question to get too messy

Using Glide to load bitmap into ImageView

@rookiedev is right, there's no load(Bitmap) in Glide, for a reason: acquiring a Bitmap usually takes time and sometimes blocking I/O. So it's not good practice to call imageWithText on the UI thread. Update: That being said I proposed this feature a while back; and while the hacks are easier to do you can find the "Glide way" below, which I highly recommend.

Glide is designed to be flexible and this problem demonstrates that trait extremely well. The following implementation may seem long, but all pieces have their reason for existence. Given the performance gain, this amount of code to fit your generator into Glide's world is not much. I tried to format it to be short, collapsing irrelevant parts and using static imports to be shorter (see the end for imports).

The code also includes programmatically generated UI so you can just copy-paste all the below code into GlideGeneratedImageListFragment.java and run it; the only external dependency is support lib's RecyclerView.

class GeneratingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// See https://docs.google.com/drawings/d/1KyOJkNd5Dlm8_awZpftzW7KtqgNR6GURvuF6RfB210g/edit?usp=sharing
// ModelType/A, DataType/T, ResourceType/Z, TranscodeType/R
private final GenericRequestBuilder<GenerateParams, GenerateParams, Bitmap, GlideDrawable> generator;

public GeneratingAdapter(final Context context) {
generator = Glide // this part should be cleaner in Glide 4.0, but that's not released yet
.with(context)
.using(new GenerateParamsPassthroughModelLoader(), GenerateParams.class) // custom class
.from(GenerateParams.class)
.as(Bitmap.class)
.transcode(new BitmapToGlideDrawableTranscoder(context), GlideDrawable.class) // builtin
.decoder(new GenerateParamsBitmapResourceDecoder(context)) // custom class
.encoder(new BitmapEncoder(Bitmap.CompressFormat.PNG, 0/*ignored for lossless*/)) // builtin
.cacheDecoder(new FileToStreamDecoder<Bitmap>(new StreamBitmapDecoder(context))) // builtin
//.placeholder(new ColorDrawable(Color.YELLOW)) // you can pre-set placeholder and error
.error(new ColorDrawable(Color.RED)) // so it's easier when binding
//.diskCacheStrategy(DiskCacheStrategy.NONE) // only for debugging to always regenerate
//.skipMemoryCache(true) // only for debugging to always regenerate
;
}
@Override public int getItemCount() { return 1000; }

private final float[] colorCache = new float[] {0, 1.0f, 0.5f};
private final float[] bgCache = new float[] {0, 0.5f, 1.0f};
@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
colorCache[0] = bgCache[0] = (position * 15) % 360; // just to have a fancy example :)
GenerateParams params = new GenerateParams(
// omit position to see Glide caching in action (every 24th item / 12th row is the same)
"android text"/* + " #" + position*/,
Color.HSVToColor(colorCache),
Color.HSVToColor(bgCache)
);
generator/*.clone() in case you see weird behavior*/.load(params).into((ImageView)holder.itemView);
}

@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int height = parent.getContext().getResources().getDisplayMetrics().heightPixels / 3;
ImageView view = new ImageView(parent.getContext());
view.setLayoutParams(new RecyclerView.LayoutParams(MATCH_PARENT, height));
view.setScaleType(ImageView.ScaleType.FIT_CENTER);
return new RecyclerView.ViewHolder(view) {}; // anon class for brevity
}
}

public class GlideGeneratedImageListFragment extends Fragment {
@Override public @Nullable View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView view = new RecyclerView(container.getContext());
view.setLayoutParams(new MarginLayoutParams(MATCH_PARENT, MATCH_PARENT));
view.setLayoutManager(new GridLayoutManager(container.getContext(), 2 /*columns*/));
view.setAdapter(new GeneratingAdapter(view.getContext()));
return view;
}
}

/** Extracted params from imageWithText, but left some hardcoded values like 20sp/bold/center in {@link Generators}. */
class GenerateParams {
final String text;
final int color;
final int background;

public GenerateParams(String text, int color, int bg) {
this.text = text;
this.color = color;
this.background = bg;
}

public String getId() {
// TODO make sure it's unique for every possible instance of GenerateParams
// because it will affect how the resulting bitmap is cached
// the below is correct correct for the current fields, if those change this has to change
return String.format(Locale.ROOT, "%s-%08x-%08x", text, color, background);
}
}

/** Boilerplate because of the degeneration in ModelType == DataType, but important for caching.
* @see GeneratingAdapter#generator */
class GenerateParamsPassthroughModelLoader implements ModelLoader<GenerateParams, GenerateParams> {
@Override public DataFetcher<GenerateParams> getResourceFetcher(final GenerateParams model, int width, int height) {
return new DataFetcher<GenerateParams>() {
@Override public GenerateParams loadData(Priority priority) throws Exception { return model; }
@Override public void cleanup() { }
@Override public String getId() { return model.getId(); }
@Override public void cancel() { }
};
}
}

/** Handles pooling to reduce/prevent GC lagging from too many {@link Bitmap#createBitmap}s */
class GenerateParamsBitmapResourceDecoder implements ResourceDecoder<GenerateParams, Bitmap> {
private final Context context;
public GenerateParamsBitmapResourceDecoder(Context context) { this.context = context; }
@Override public Resource<Bitmap> decode(GenerateParams source, int width, int height) throws IOException {
BitmapPool pool = Glide.get(context).getBitmapPool();
Bitmap bitmap = pool.getDirty(width, height, Bitmap.Config.ARGB_8888);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
Generators.imageWithTextNoLayout(context, bitmap, source);
return BitmapResource.obtain(bitmap, pool);
}
@Override public String getId() {
// be careful if you change the Generator implementation you have to change this
// otherwise the users may see a cached image; or clear cache on app update
return "com.example.MyImageGenerator";
}
}

class Generators {
/** OP's original implementation fixed for real centering */
public static Bitmap imageWithText(Context context, Bitmap bitmap, GenerateParams params) {
TextView view = new TextView(context);
view.setText(params.text);
view.setTextColor(params.color);
view.setBackgroundColor(params.background);
view.setTypeface(null, Typeface.BOLD);
view.setGravity(Gravity.CENTER);
view.setTextSize(20 /*sp*/);
Canvas canvas = new Canvas(bitmap);
view.measure(makeMeasureSpec(canvas.getWidth(), EXACTLY), makeMeasureSpec(canvas.getHeight(), EXACTLY));
view.layout(0, 0, canvas.getWidth(), canvas.getHeight());
view.draw(canvas);
return bitmap;
}

/** Generate centered text without creating a View, more lightweight.
* Consider https://stackoverflow.com/a/8369690/253468 for multiline support. */
public static Bitmap imageWithTextNoLayout(Context context, Bitmap bitmap, GenerateParams params) {
Paint paint = new Paint();
paint.setColor(params.color);
paint.setTextAlign(Paint.Align.CENTER); // text's anchor for the x given in drawText
paint.setTextSize(applyDimension(COMPLEX_UNIT_SP, 20, context.getResources().getDisplayMetrics()));
paint.setTypeface(Typeface.DEFAULT_BOLD);

Canvas canvas = new Canvas(bitmap);
canvas.drawColor(params.background);
canvas.drawText(params.text, canvas.getWidth() / 2, canvas.getHeight() / 2, paint);
return bitmap;
}
}

// Here are the imports in case you need it;
// didn't want to put it in the beginning to keep the relevant code first.

import java.io.IOException;
import java.util.Locale;

import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.*;
import android.view.*;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.*;

import static android.util.TypedValue.*;
import static android.view.View.MeasureSpec.*;
import static android.view.ViewGroup.LayoutParams.*;

import com.bumptech.glide.*;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.resource.bitmap.*;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.file.FileToStreamDecoder;
import com.bumptech.glide.load.resource.transcode.BitmapToGlideDrawableTranscoder;

Here's how it looks like (real scrolling is much smoother, GIF is really low FPS):

Sample Image

Notice how it loads the first few items and then gradually loads the rest. It takes a little that the memory cache and pool warms up, but you can use a preloader if you want even smoother display. After it's warmed up it scrolls nicely. The delete button on the action bar calls Glide.clearDiskCache() and Glide.clearMemory() so it starts regenating the items again.

Save picture to storage using Glide

Update

1.Permissions in manifest file

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  1. Need Coroutine to do some background work
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'

  1. Glide to get the image as a bitmap
CoroutineScope(Dispatchers.IO).launch {
saveImage(Glide.with(this@MainActivity)
.asBitmap()
.load("https://i.imgur.com/4HFRb2z.jpg") // sample image
.placeholder(android.R.drawable.progress_indeterminate_horizontal) // need placeholder to avoid issue like glide annotations
.error(android.R.drawable.stat_notify_error) // need error to avoid issue like glide annotations
.submit()
.get())
}

  1. Save the image in a newly created folder with the specific name
private fun saveImage(image: Bitmap): String? {
var savedImagePath: String? = null
val imageFileName = "JPEG_" + "FILE_NAME" + ".jpg"
val storageDir = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
.toString() + "/YOUR_FOLDER_NAME"
)
var success = true
if (!storageDir.exists()) {
success = storageDir.mkdirs()
}
if (success) {
val imageFile = File(storageDir, imageFileName)
savedImagePath = imageFile.getAbsolutePath()
try {
val fOut: OutputStream = FileOutputStream(imageFile)
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut)
fOut.close()
} catch (e: Exception) {
e.printStackTrace()
}

// Add the image to the system gallery
galleryAddPic(savedImagePath)
//Toast.makeText(this, "IMAGE SAVED", Toast.LENGTH_LONG).show() // to make this working, need to manage coroutine, as this execution is something off the main thread
}
return savedImagePath
}

  1. Add the image to the gallery and do send broadcast (not mentioning much about as it's out of the question)
 private fun galleryAddPic(imagePath: String?) {
imagePath?.let { path ->
val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
val f = File(path)
val contentUri: Uri = Uri.fromFile(f)
mediaScanIntent.data = contentUri
sendBroadcast(mediaScanIntent)
}
}

Original

1.Permissions in manifest file

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2.Glide to download the image as a bitmap

Glide.with(mContext)
.load("YOUR_URL")
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
saveImage(resource);
}
});

  1. Save the bitmap into your memory
    private String saveImage(Bitmap image) {
String savedImagePath = null;

String imageFileName = "JPEG_" + "FILE_NAME" + ".jpg";
File storageDir = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/YOUR_FOLDER_NAME");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}

// Add the image to the system gallery
galleryAddPic(savedImagePath);
Toast.makeText(mContext, "IMAGE SAVED", Toast.LENGTH_LONG).show();
}
return savedImagePath;
}

private void galleryAddPic(String imagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}


Related Topics



Leave a reply



Submit