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.os.Environment;
import java.io.File;
import java.io.FileOutputStream;

public class Main {
    public static final String IMAGE_PATH = Environment.getExternalStorageDirectory().getPath() + "/cat_image";

    public static String saveBitmapInSdCard(Bitmap bitmap, String fileName) {
        FileOutputStream fos = null;
        File file = null;
        String path;
        try {
            file = new File(createFile(IMAGE_PATH, fileName));
            fos = new FileOutputStream(file);
            if (fos != null) {
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return file.getAbsolutePath();
    }

    public static String createFile(String parent, String fileName) {
        File dir = new File(parent);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, fileName);
        return file.getAbsolutePath();
    }
}