Do base64 Encode ImageView - Android User Interface

Android examples for User Interface:ImageView

Description

Do base64 Encode ImageView

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;

import android.util.Base64;
import android.widget.ImageView;

public class Main {
    public static String base64Encode(ImageView imageView) {
        if (imageView.getDrawable() != null) {
            Bitmap avatarBitmap = ((BitmapDrawable) imageView.getDrawable())
                    .getBitmap();//from   w  w w . j a va2 s .  c  o m
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            avatarBitmap.compress(Bitmap.CompressFormat.PNG, 70, baos);
            byte[] avatarByteArray = baos.toByteArray();
            return Base64.encodeToString(avatarByteArray, Base64.DEFAULT);
        } else {
            return null;
        }
    }
}

Related Tutorials