Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

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

public class Main {
    /**
     * @author Casper Rasmussen - 2012
     * Use this method to get byte[] from a file of a given path.
     * @param filePath
     * @param compressRate - 0-100%, 100% means no compression
     * @param mCompressFormat - CompressFormat.PNG, CompressFormat.JPEG etc.
     * @return byte[]
     */
    public static byte[] filePathToByteArray(String filePath, int compressRate, CompressFormat mCompressFormat) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        Bitmap bitmapFromFile = BitmapFactory.decodeFile(filePath, options);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmapFromFile.compress(mCompressFormat, compressRate, stream);
        byte[] byteArray = stream.toByteArray();

        return byteArray;
    }
}