As a .net developer, new to android, I'm trying to consume images from my asmx webservice
. Images are saved in the db as nvarchar
(bytearray), so my webservice returns a string that can be converted into bitmap. This all works fine but I run into problems trying to get png images with transparent parts in it. No matter what I try, the transparent parts are always white. The problem is, there could be a couple of things I'm doing wrong here...
This is the way I write my png to db (c#) (it could be I'm losing transparancy here already?):
Image toAdd = Image.FromFile(@"I:\Andoid\h1graph.png");
string imagestring = System.Convert.ToBase64String(imageToByteArray(toAdd));
And this is the way I consume it on my android app :
Bitmap bmp;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
String result = resultsRequestSOAP.toString();
if (!result.equals("")) {
byte[] bloc = Base64.decode(result, Base64.DEFAULT);
bmp = BitmapFactory.decodeByteArray(bloc, 0, bloc.length);
bmp.setHasAlpha(true);
ByteArrayOutputStream os = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
byte[] bloc2 = os.toByteArray();
Bitmap bPNGCompress = BitmapFactory.decodeByteArray(bloc2, 0, bloc2.length);
viewModel.setThreeD(bPNGCompress) ;
}
I've stripped the code so only the essential code is there. So, this works but it doesn't keep transparency.
I'm guessing, it's because the BitmapFactory.DecodeByteArray
creates a bitmap without transparency, and so the rest of the conversion code won't create transparency.
I'm struggling for days now, trying to find solutions but I don't seem to find it. There must be a best practice for consuming png images from a webservice, serialized as string (?).
Using png from the resources, works ok with transparency, so I know my png is transparent and my UI controls are ok. (I know the coding is not state of the art but to avoid having to copy paste all kinds of methods, I replaced some code, that is normally in methods, to make it more readable)
I'm at the start of a project, experimenting with android, so If anyone has a completely different idea on how te get images into my app, be free to post ideas.
Try explicit alpha configuration for the decode operation:
BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
//and then call decodeByteArray with the explicit alpha option:
bmp = BitmapFactory.decodeByteArray(bloc, 0, bloc.length, opts);
//and then apply the same options to the compressed bmp version:
Bitmap bPNGCompress = BitmapFactory.decodeByteArray(bloc2, 0, bloc2.length);
Update I have written a simple test app to test the code and it works correctly (ie, has transparency). It's 100% that you lost the ALPHA bits before processing the image.
Placeholder layout:
<ImageView android:background="#FF0000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_marginTop="135dp" />
Code: ImageView imageView = (ImageView) findViewById(R.id.imageView);