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 android.content.Context;

import android.graphics.Bitmap;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class Main {

    public static File getFile(Context context, Bitmap sourceImg) {
        try {
            File f = new File(context.getCacheDir(), System.currentTimeMillis() + "temp.jpg");
            f.createNewFile();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int options = 100;
            sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos);
            while (bos.toByteArray().length / 1024 > 100) {
                bos.reset();
                options -= 10;
                sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos);
            }
            byte[] bitmapdata = bos.toByteArray();
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
            return f;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}