Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static boolean saveBitmapToSDCard(Bitmap bitmap, String filePath, String fileName) {
        boolean flag = false;
        if (null != bitmap) {
            try {
                fileName = fileName + ".jpg";
                File file = new File(filePath);
                if (!file.exists()) {
                    file.mkdirs();
                }
                File f = new File(filePath + fileName);
                if (f.exists()) {
                    f.delete();
                }
                BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(f));
                bitmap.compress(CompressFormat.JPEG, 100, outputStream);
                outputStream.flush();
                outputStream.close();
                flag = true;
            } catch (FileNotFoundException e) {
                flag = false;
            } catch (IOException e) {
                flag = false;
            }
        }
        return flag;

    }
}