Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            avatarBitmap.compress(Bitmap.CompressFormat.PNG, 70, baos);
            byte[] avatarByteArray = baos.toByteArray();
            return Base64.encodeToString(avatarByteArray, Base64.DEFAULT);
        } else {
            return null;
        }
    }
}