Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Locale;
import android.graphics.Bitmap;

public class Main {
    public static boolean writeBmpToSDCard(Bitmap bmp, File file, int quality) {
        try {
            ByteArrayOutputStream baosm = new ByteArrayOutputStream();
            if (file.getPath().toLowerCase(Locale.getDefault()).endsWith(".png")) {
                bmp.compress(Bitmap.CompressFormat.PNG, quality, baosm);
            } else {
                bmp.compress(Bitmap.CompressFormat.JPEG, quality, baosm);
            }
            byte[] bts = baosm.toByteArray();

            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            File tempFile = new File(file.getPath() + ".png");

            FileOutputStream fosm = new FileOutputStream(tempFile);
            BufferedOutputStream bos = new BufferedOutputStream(fosm);
            bos.write(bts);
            bos.flush();
            bos.close();
            fosm.close();

            tempFile.renameTo(file);

            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return false;
    }
}