Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.util.Base64;

import java.io.ByteArrayOutputStream;

public class Main {
    public static String imgsToBase64(String filePath) {
        Bitmap bitmap = BitmapFactory.decodeFile(filePath);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String lastName = filePath.substring(filePath.lastIndexOf(".") + 1);
        if (lastName.equals(Bitmap.CompressFormat.JPEG)) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        } else {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        }
        return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
    }

    public static String imgsToBase64(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

        return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
    }
}