2

I have a variable Bitmap image. I have imageButton in my fragment. I can change image of imageButton with Glide.with().load().into(). But I want not to just change image but to save it into Bitmap variable so I can use it for some other task. I tried this

Bitmap image = Glide.with(getContext()).asBitmap().load(imagePath).submit().get();
imageButton.setImageBitmap(image);

But image of imageButton does not change. What's wrong with first line then? Because I am pretty sure that problem is there but don't understand yet what is exactly wrong.


A solution is to do

Bitmap image = BitmapFactory.
        decodeStream(getContext().
        getContentResolver().
        openInputStream(selectedImage));
imageButton.setImageBitmap(image);

Where selectedImage is Uri selectedImage = data.getData() of onActivityResult method, but it will be total mess because of size of bitmap, so we have to use createScaledBitmap, I don't like it. For some reason Bitmap image = BitmapFactory.decodeFile(imagePath) doesn't work, imageButton just becomes tiny grey square, and I didn't find a solution for this.

Zoe
  • 27,060
  • 21
  • 118
  • 148

2 Answers2

0

first of all why are using the imageButton..use imageView it will good than imagebutton and it will work

gowtham6672
  • 999
  • 1
  • 10
  • 22
0

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