Jes
Jes

Reputation: 2778

Downloaded Bitmap loses transparency when added to an ImageView

I am trying to show a potentially transparent image from a remote location, but the alpha channel seems to be colored white when I add it to my ImageView.

I am downloading a remote image with the following code:

public static Bitmap loadBitmap(String url) throws IOException {
   int bufferSize = 1024;
   InputStream in  = new BufferedInputStream(new URL(url).openStream(), bufferSize);
   Bitmap bitmap = BitmapFactory.decodeStream(in);

   try {
      in.close();
   } catch (Exception ignored) {

   }
   return bitmap;
}

In my Activity with the mentioned ImageView I have the following code (try-catch omitted):

if (bitmap != null) {
    ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
    imageView.setImageBitmap(bitmap);
}

bitmap.getConfig() returns ARGB_8888.

I am coding against Android 1.6, i.e. SDK version 4.

Am I missing some magic setter? When I load the exact same picture as a Drawable from my res folder it works fine.

I noticed a setter called setHasAlpha on Bitmap, but this is since SDK level 12.

EDIT: I tried getting the color of some of the pixels I know are transparent, and their color == 0, which is transparent.

Upvotes: 2

Views: 1342

Answers (2)

kapil
kapil

Reputation: 257

use compress format as PNG

BufferedOutputStream bos = new BufferedOutputStream(
                fileOutputStream);
        bmpimg.compress(CompressFormat.PNG, 20, bos);

Upvotes: 0

Sajid
Sajid

Reputation: 4421

Sounds like the ImageView itself has a background. Try setting the background color to transparent (eg, imageView.setBackgroundColor(0);

Upvotes: 3

Related Questions