Why Bitmap to Base64 String Showing Black Background on Webview in Android

Why Bitmap to Base64 String showing black background on webview in android?

The JPEG format does not support alpha transparency, which is why the transparent background becomes black when you convert your original image to JPEG.

Use the PNG format instead:

 map1.compress(Bitmap.CompressFormat.PNG, 100, baos); 

and

String imgTag = "<img src='data:image/png;base64," + imgToString               
+ "' align='left' bgcolor='ff0000'/>";

Android: How to Display a Bitmap in a WebView?

Using the link from itsrajesh4uguys, I've created this code snippet:

// Desired Bitmap and the html code, where you want to place it
Bitmap bitmap = YOUR_BITMAP;
String html="<html><body><img src='{IMAGE_PLACEHOLDER}' /></body></html>";

// Convert bitmap to Base64 encoded image for web
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
String image = "data:image/png;base64," + imgageBase64;

// Use image for the img src parameter in your html and load to webview
html = html.replace("{IMAGE_PLACEHOLDER}", image);
webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", "");

Android TV: unfocused image contains black transparent background?

I found a way to fix this. VertiGridPresenter's dimmer default is true , set it to false like:

VerticalGridPresenter gridPresenter = new VerticalGridPresenter(FocusHighlight.ZOOM_FACTOR_NONE, false);

or change the theme item <item name="overlayDimDimmedLevel">10%</item>
in @style/Theme.Leanback, the percent value set the transparent value when set 100% the background will black.

Is this a bug for BitmapFactory while decoding InputStream?

Have you tried

Options options = new BitmapFactory.Options(); 
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(activityName.getResources(), R.id.image, options);

Instead of:

 cs = Bitmap.createBitmap(backgroungImage.getWidth(), backgroungImage.getHeight(), backgroungImage.getConfig()); 

try:

cs = Bitmap.createBitmap(backgroungImage.getWidth(), backgroungImage.getHeight(), Bitmap.Config.ARGB_8888);

how to improve image quality on Canvas.drawBitmap while merging two images?

oh yes, I just make the 3 images of hdpi,mdpi,ldpi "+" image with appropriate resolution. and it works for me.

How to load image (web_image) in Android WebView

f**k.I forgot add this code in the AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />



Related Topics



Leave a reply



Submit