Convert Bitmap/Drawable to Drawable (int)

Asked

Viewed 1,064 times

0

I need to convert a Bitmap or even a Drawable into a Drawable (int), because I’m using a Bootstrap for android that needs an object of this type, but as I’m downloading the image of a url and not picking it up internally, I’m not getting it.

Follow the passage:

Bitmap bitIcon = BitmapFactory.decodeStream(url.openConnection().getInputStream());
Drawable drawable = new BitmapDrawable(getResources(), bitIcon);
imgPerfil.setImage();

In the third line it asks for an integer, but I only have the Drawable Object and not its id. I wondered how to search this integer.

  • follow the link to the bootstrap-android library I’m using https://github.com/Bearded-Hen/Android-Bootstrap/wiki/Bootstrap-Thumbnail

1 answer

1

To convert a Bitmap in Drawable do the following:

Drawable seuDrawable = new BitmapDrawable(getResources(),seuBitmap);

However, as you requested, you need to get the Drawable by id then use:

Bitmap seuBitmap = (Bitmap) findViewById(R.id.seuIdDoDrawable);
Drawable seuDrawable = new BitmapDrawable(getResources(),seuBitmap);
  • But I need Drawable’s id, like it’s being searched internally

  • see my edition in reply @lucasrafagnin

  • I’m downloading Bitmap from an external link, and converting it to Drawable. I don’t have it internally

  • So you already have one Drawable, what would be your problem??

  • Actually my question got confused, I have the Drawable object. But in the setImage() method it calls for an int, and I’m not getting this conversion.

  • The int you are talking about is the int returned by R.drawable.seuDrawable but you don’t have it in the View yet, how about adding it to the view? Later you can redeem the ID.

  • You must save Drawable to the folder /res/drawable for it to become a Resource and then you will have a type ID R.drawable.seuDrawable.

  • Okay, and how would I make this recording?

  • The problem is that it is impossible to write to the folder res/drawable at runtime, I’m thinking of another way for you to rescue the ID...

  • Try to use drawable.createFromStream(url.openConnection().getInputStream(), testDrawable) and then check whether R.drawable.testDrawable exists.

  • did not work, but I decided to do otherwise, and not use the bootstrap in this case. Thanks for the help.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.