0

Glide 4.11+ only

With Glide, I can do this and get a bitmap, which is awesome,

protected void onActivityResult(int requestCode, int resultCode, Intent tent){
  super.onActivityResult(requestCode, resultCode, data);
  Glide.with(this).asBitmap()
  .load(tent.getData())
  .apply(new RequestOptions().override(300, 400))
  .into(new CustomTarget<Bitmap>() {
    @Override
    public void onResourceReady(@NonNull Bitmap glideBitmap,
              @Nullable Transition<? super Bitmap> transition) {

      Log.i("c", "BAM! Just like that, a bitmap ...");

Awesome.

However, often I need an actual bite array,

I do this ..

protected void onActivityResult(int requestCode, int resultCode, Intent tent){
  super.onActivityResult(requestCode, resultCode, data);
  Glide.with(this).asBitmap()
  .load(tent.getData())
  .apply(new RequestOptions().override(300, 400))
  .into(new CustomTarget<Bitmap>() {
    @Override
    public void onResourceReady(@NonNull Bitmap glideBitmap,
              @Nullable Transition<? super Bitmap> transition) {

      // convert badly to a byte array...

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      glideBitmap.compress(Bitmap.CompressFormat.JPEG, 70, baos);
      byte[] byteData = baos.toByteArray();

      Log.i("c", "had to work for bytes :/ ...");

I assume real droid programmers laugh at me?

Is there a more direct way to get a byte array from Glide?


Use case - am sending this up to Firebase in the usual byteish way,

  UploadTask uploadTask = storeRef.putBytes(byteData);

Maybe I am XY, can Glide actually return a stream? As you know to send to Firebase you need either a stream or byte data. If so, sorry for being a lame-ass ios dev.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 1
    In this article here: https://github.com/bumptech/glide/wiki scroll down to the section or search `Transcoding` where you'll see `toBytes` method. Maybe this can help you. – Mayur Gajra Mar 30 '21 at 16:40
  • thahk you so much @MayurGajra - ah, but there's no way to tell Glide "jpeg, 70%" ? – Fattie Mar 30 '21 at 18:20

1 Answers1

1

You ought to replace asBitmap with as(byte[].class).

sdex
  • 3,169
  • 15
  • 28
  • Thanks again - as I was asking Mayur, do you know if there's a way to tell Glide "jpeg, 70%" ? (or PNG etc?) – Fattie Mar 30 '21 at 18:21
  • 1
    @Fattie I think you need custom `ResourceDecoder`. Check this question https://stackoverflow.com/q/54360199/2894324 – sdex Mar 31 '21 at 07:08