base64 image and html.fromhtml android - android

My data are stored in html format on db.
The image are stored in base64 format (as a string in db).
I am trying to show this data.
My textView is setted like this:
setText(Html.fromHtml(content));
all html tags in 'content' are being displayed correctly. Except 'img' tag (which contains a base64 encode image).
So, my question is:
the tag 'img' of 'Html.fromHtml' can decode a string with base64 image?
p.s: The place where the tag is show just a little gray square. No got errors.
thx.

Use Html.fromHtml combined with your own implementation of Html.ImageGetter.
See here.
When overriding Html.ImageGetter.getDrawable, convert the Base64 string into a byte array (you can use android.util.Base64) and feed it into BitmapFactory.decodeByteArray to produce a Bitmap which you can then pass into the constructor of a BitmapDrawable to return.
For example:
Html.fromHtml(content, new Html.ImageGetter() {
#Override
public Drawable getDrawable(String source) {
byte[] data = Base64.decode(source, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
return new BitmapDrawable(getResources(), bitmap);
}
}, null);

Related

Wrong Images are displayed from sqlite Database into ListView

I'm downloading some text and images from the server using AsyncTask. I'm storing the texts and images in sq-lite database. I'm storing in the form of byte array.
In post Execute method of AsyncTask I store the convert the images in byte array and store them in database using for loop.
Problem is when I display data in List View from database sometimes it displays the correct images but some times it displays wrong images. I'm using a handler to delay the information into List View by 8 seconds. I've no idea why sometimes it shows the correct images and sometimes it shows wrong images.
What I'm doing wrong? How can I display the correct image every time. Moreover Can anyone help me how can I avoid the log cat message :
"Skipped XXX frames. The application is doing to much work in main thread."
I'm using fragments. As the application starts it will get data from server and which includes URL to Image. I'm using that URL to download images. Storing both the texts and images in database and showing it in List View in onPostExecute method.
Below are the codes I've used:
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
In adapter class where I'm extending CursorAdapter class. I'm getting byte array from sq-lite database and converting it in image and display it to List View.
Code to convert from byte array to bitmap is below:
private static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
I'm setting it to list View item like this:
byte [] image = cursor.getBlob(imageColumnIndex);
if(image == null)
{
}else{
Bitmap bitmap = getImage(image);
imageView.setImageBitmap(bitmap);
}

Save RelativeLayout with content as an image

I have relativelayout (A template) where it contain textboxes whose content is populated at runtime.On clicking save button,I need to save the template as an image in SD card.Is it possible.
I referred the below link:
How do I convert a RelativeLayout with an Imageview and TextView to a PNG image?
But the image saved cannot be opened.
Is my requiremnet possible.Or else please advice how can I achieve it.
I am behind this for several days.I am new to ANdroid.Please help.
Thanks in Advance.
You can pass any view or layout to devBitmapFrmViewFnc function and get the bitmap. You can save the bitmap in jpeg using devImjFylFnc.
|==| Dev Bitmap Image from View :
Bitmap devBitmapFrmViewFnc(View viewVar)
{
viewVar.setDrawingCacheEnabled(true);
viewVar.buildDrawingCache();
return viewVar.getDrawingCache();
}
|==| Create a JPG File from Bitmap :
static void devImjFylFnc(String MobUrlVar, Bitmap BitmapVar)
{
try
{
FileOutputStream FylVar = new FileOutputStream(MobUrlVar);
BitmapVar.compress(Bitmap.CompressFormat.JPEG, 100, FylVar);
FylVar.close();
}
catch (Exception ErrVar) { ErrVar.printStackTrace(); }
}

How to get the profile picture from ProfilePictureView to store in the database

I have successfully integrated facebook login in my android app.So I am displaying user profile data like name,email and picture.
Now I need to store user data such as name,email and profile picture in database. I know how to get the name and email since it can be get from TextViews. But I am struggling to get the profile picture.
The profile picture is displayed using ProfilePictureView.
I tried to convert that image on ProfilePictureView to byte array, but it is not working.(Normally this works with ImageView ,Imagebuttons, but it is not working with ProfilePictureView)
pic.setProfileId(user.getId());
ImageView imageView = (ImageView)pic.getChildAt(0);
Bitmap bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
image_ = convertImageToByte(bmp); //image_ is a byte[]
private byte[] convertImageToByte(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
return stream.toByteArray();
}
I am getting an image like this instead of the real profile picture.
Is there anyway to store the image? (like a link to get the profile picture to store so I can retrieve the image using the link at later time)
Thanks.
Try this. It worked for me
private ProfilePictureView profilepic;
In OnCreate Method type:
profilepic = (ProfilePictureView) findViewById(R.id.<profilepicture id>);
profilepic.setProfileId(id of user (string));
Hope it helps
i used this and it's working ok:
profileImageView = ((ImageView)faceBookProfilePictureView.getChildAt(0));
bitmap = ((BitmapDrawable)profileImageView.getDrawable()).getBitmap();
circular.setImageBitmap(bitmap);
where "circular" it's CircularImageView Object.

What should be the original ID of thumbnails in MediaStore.Images.Thumbnails methods?

I'm trying to get the thumbnails of some images but the documentation is very unclear about what the origId should be. I have a simple method that takes a file as parameter and returns a drawable for thumbnails like this:
Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(), Long.parseLong(Uri.fromFile(file).getLastPathSegment()), Thumbnails.MINI_KIND, null);
BitmapDrawable bd = new BitmapDrawable(mContext.getResources(), thumbnail);
return bd;
But I get a java.lang.NumberFormatException: Invalid long exception whenever getThumbnail is called.
So how should I obtain the correct origId for an image file?
Try this way (this worked for me):
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);

How do I get the initial url of an ImageView formed from a Bitmap

Initially I created a bitmap from an external URL by using a bitmapFactory. I then put the bitmap in an Image view, like:
ImageView mim = (ImageView) findViewById(R.id.moviepix);
String mi = "http://xxxxxxx.com/"+ result.movie_pix;
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(mi).getContent());
mim.setImageBitmap(bitmap);
Now I need to reverse engineer the url from the ImageView.
All help is appreciated.
You can't, but you can however store the URL for later use with ImageView.setTag()

Categories

Resources