Losing transparent background when downloading an external PNG image - android

I'm displaying an external image in an image view by fist downloading it like this:
bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
Then setting this bitmap to the ImageView like this:
imageView.setImageBitmap(bitmap);
This works all good except that one of the images is a PNG and I lose the transparent background when using the BitmapFactory.
Can anyone tell me how I can keep the transparent background?

not sure if this will help, but try following this advice and adding Options to make sure that your image is pulled in as ARGB_8888
http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String, android.graphics.BitmapFactory.Options)

Related

(Android) How to download an image/gif without a black background?

I am using Glide to get an image as a byte array as such
Glide.with(context)
.as(byte[].class)
.load(url)
.submit()
.get();
and it works well with an image with a background, as well as GIFs. However, when there is a static image with a transparent background (.PNG), it will produce a black background instead of keeping its transparency. I know you can avoid this problem by using a bitmap, but I want to retain both .PNG and .GIF files. Is there a possible way to retain the transparent background when loading a byte array using Glide?

Remove transparent white background from gif image using glide?

How can I remove white background around image when loading with glide.. You can find out below code as well as screenshot;
Glide.with(thiscontext).load(R.drawable.badge_green_yellow)
.asGif().crossFade()
.diskCacheStrategy(DiskCacheStrategy.SOURCE).into(holder.ivHourlyIcon);
ScreenShot
Put your GIF into the raw folder (res/raw/badge_green_yellow.gif)
and then load it like this
Glide.with(thiscontext).asGif().load(R.raw.badge_green_yellow).into(holder.ivHourlyIcon);

Bitmaps lose transparency when downloaded with Glide

I am trying to download a bitmap image in an android app using the Glide library (version 4), but when I do that the transparency of the image is lost and it is replaced by a white background.I tried using the commands:
Glide.with(this).load(url).into(imageView);
Glide.with(this).asBitmap().load(url).into(imageView);
and
Glide.with(this).load(url).apply( new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888).encodeFormat(Bitmap.CompressFormat.PNG)).into(imageView);
However nothing seems to work.
The image url is: https://s151.convertio.me/p/HwxwbAP4nbAyKsEmdn7ORw/1dc5e4a91d3de94ef6c5fcd51b0fb79d/yellow_circle_1.bmp
And here are some screenshots of the app:
This is how it should look.
(It looks like that when I use a png image, the red background is the ImageView's background and the image background is transparent.)
This is how it looks with a bitmap
(The transparent background became white.)
Thanks a lot.
There is no alpha channel in BMP format, as far as I know
https://en.wikipedia.org/wiki/BMP_file_format
Glide.with(context).load(dataModel.getImgurl())
.thumbnail(0.5f)
.fitCenter()
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);

MonoDroid Image Banding

Im having an issue with my backgrounds. I get horrible image banding when setting background images with gradients. When I set the background in my root layout I do it as follows:
android:background="#drawable/GradientImage"
I have also tried setting the background of my root layout in the code (after removing background from axml):
Window.SetFormat(Android.Graphics.Format.Rgbx8888);
Window.AddFlags(Android.Views.WindowManagerFlags.Dither);
BitmapFactory.Options options = new BitmapFactory.Options();
options.InPreferredConfig = Bitmap.Config.Argb8888;
Bitmap gradient = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Background_640,options);
sv.SetBackgroundDrawable(new BitmapDrawable(gradient));
This is done in OnCreate and unfortunately does not fix the issue,
Has anyone ever came across this issue? Does anyone now how to fix this.
This code works for me (in onCreate())
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
Also, when using PNG images, make sure they are using 32-bit color depth,
because some image editing programs save them as 24-bit by default.
For example, in GIMP, you can force the PNG image to be saved as 32-bit
by adding alpha channel.

9-patch bitmap on a widget using RemoteViews

I have a widget with an ImageView on it. I set this ImageView to a Bitmap created from a 9-patch PNG resource. The image is set correctly but is not stretched correctly - i.e. the whole image is stretched instead of just part like defined in my 9-patch PNG. If I just set the 9-patch image as a resource, it works. How can I fix this?
// Does not work (9-patch does not display correctly)
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.someNinePatch);
remoteViews.setImageViewBitmap(R.id.someImageView, bitmap);
// Works (9-patch displays correctly)
remoteViews.setImageViewResource(R.id.someImageView, R.drawable.someNinePatch);
Android knows a PNG is a nine-patch by virtue of the .9.png file extension on the resource. I do not know of any way to use nine-patch images outside of resources, because Android will not have the file extension and will not know the image is a nine-patch.

Categories

Resources