Looks good?
public static Bitmap stringToImage(String base64) {
byte[] decodedString = decode(base64, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
}
this code returns null, if my base64 string starts with "data:image/jpeg;base64,". but if I remov this prefix - this code works fine! How to solve this problem?
I tested my base64 string and it works fine (comment 1 How to display Base64 images in HTML?)
Unless I'm confused, "data:image/jpeg;base64," is not a valid part of a base64 string. If that's the case, it's really no wonder it's not decoding correctly. Just remove it from the head of the string before you decode if it's causing problems.
u may use before passing the String to function
String base64="";
base64.replaceAll("data:image/jpeg;base64,", "");
pls change given string according to ur string
Related
can you give me some reference about imageSlider from json?
I can already convert base64 to bitmaps using listView. but I am confused about how to display the results of the conversion into imageSlider.
data:[{"ID": 1,"Title": "Ads","Image":"data:image\/jpeg;base64........"]
This method can help:
private void setExistImage(ImageView imageView, String base64String){
if (!base64String.isEmpty()) {
byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
}
}
Maybe you can give a try to retrofit or picasso they are most popular libraries used in Android nowadays
I am hitting server and getting some data in string format.
in this data there are some special character like - ' . but when i set that string in textview these special character convert into ? .
So how can i avoid this issue ? please help.
first try :
String t = "<![CDATA["+title+"]]>";
mTitle.setText(Html.fromHtml(text));
second try :
String base64 = Base64.encodeToString(getTitle().getBytes(), Base64.DEFAULT);
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, StandardCharsets.UTF_8);
mTitle.setText(text);
Try this:
tv.setText(news_item.getTitle().replaceAll("\u2019", "'"));
Refer this link for unicode encoding.
I am getting a base64 string from web service call. I am then trying to take that string and place it into an image view within a listview. I am getting a null point exception on the line byte[]. I am not sure what I am doing wrong or why I am getting null there when I debug I am able to get the array and step through.
String peepPicData = peepWithPic.getThumbnailData();
byte[] imageAsBytes = Base64.decode(peepPicData.getBytes(), Base64.DEFAULT); //the null is here
Bitmap parsedImage = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
holder.mPeepPic.setImageBitmap(parsedImage);
I am not sure why it's null when it converts to an array and steps through. I have tried this Base64 string in a converter on a browser and it works.
I am getting the Base64 string with peepWithPic.getThumbnailData(); but then get null in bytep[].
If I take that base64 string and plug it into String peepPicData = "theactualBase64string" I get the picture over and over. Why would this not work with just getting it from the getter since its already being set before this call?
I had to add a null check because some of the peepWithPic do not acutally have pics. Here is the updated code
if (peepWithPic.getThumbnailData() != null) {
String peepPicData = peepWithPic.getThumbnailData();
byte[] imageAsBytes = Base64.decode(peepPicData.getBytes(), Base64.DEFAULT);
Bitmap parsedImage = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
holder.mPeepPic.setImageBitmap(parsedImage);
}
Basically I am parsing some JSON that has an image with it and trying to load it into a ImageView. However mBitmap is returning null. I have no idea why and further research has not helped..
Here is an example url I am working with:
http://b.thumbs.redditmedia.com/ivBAJzLMJEkEy9jgTy3z4n-mO7gIGt5mQFU1Al5kJ-I.jpg
Here is all relevant code:
public static Bitmap LoadImageFromUrl(String url){
try {
mBitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
return mBitmap;
}catch (Exception e){
Log.d(TAG,"Error getting image");
return null;
}
}
Here is where the method is called:
mListingModel.setmImageView(LoadImageFromUrl(data.getString(JSON_THUMBNAIL)));
Here is where I set the ImageView:
if(mItem.getmImageView() != null) {
holder.imageView.setImageBitmap(mItem.getmImageView());
}
Note: I am calling the method in an AsyncTask so that is not the problem.
The javadoc for Bitmap.decodeStream() states that:
If the input stream is null, or cannot be used to decode a bitmap, the
function returns null
You're passing it the results of URL.getContent(), which states in its javadoc:
By default this returns an InputStream, or null if the content type of
the response is unknown.
So maybe you should check to see if getContent() returns null before passing it on to the decoder.
i had in the past stupid problems like having my stream reached the end before i decoded it, so ensure to set it's position to 0 before decoding the stream
eg.
stream.Position = 0;
var bmp = BitmapFactory.DecodeStream(stream);
Can you check if this works for you:
InputStream in = new java.net.URL(downloadURL).openStream();
Bitmap avatarBmp = BitmapFactory.decodeStream(in);
in.close();
Also a reminder to add this in your Manifest
<uses-permission android:name="android.permission.INTERNET" />
I had the same problem.
I tried to find decode fail reason.. but I couldn't find.
I just confirmed that InputStream and BufferedInputStream are not null.
After restarting my AVD.. the problem was resolved even though I didn't change any code.
From my android app I am sending an image to C# server converting it to Base64
#Override
public void onPictureTaken(byte[] data, Camera camera)
{
String image = Base64.encodeToString(data, Base64.DEFAULT);
sendtoserver(image);
}
from the server side I received a string but don't know how to convert it and save it.For help I am getting this string tell me how to convert it and save it in C#
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ
Did you try using Convert.FromBase64String(string s) ?
Source: http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx
UPDATE (posted here instead of in the comments, since the code would display better)
Your Base64 representation is incorrect. For the image you gave in the comments, the Base64 representation is 339801 characters long.
I used the following code to generate a Base64 representation:
string path = #"C:\1XlqZF2.jpg";
Image img = Image.FromFile(path);
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Jpeg);
arr = ms.ToArray();
}
String b64 = Convert.ToBase64String(arr);
What is the Base64 class you are using ? I couldn't find it on MSDN.