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 java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.Matrix;

import android.net.Uri;
import android.os.Environment;

import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.ImageColumns;
import android.util.Log;

public class Main {
    /** saved image file name format */
    private static final String DATA_FORMAT = "'IMG_'yyyyMMdd_HHmmss'.jpg'";
    /** saved image file path */
    public static final String PIC_ROOT_PATH = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + File.separator;

    public static boolean storeImage(Context context, Bitmap bmp, boolean isRotate) {

        // use the current data&time for image file name
        String takenTime_YYMMDD_HHMMSS = new SimpleDateFormat(DATA_FORMAT).format(new Date());

        // saved bitmap: full path
        String path = PIC_ROOT_PATH + takenTime_YYMMDD_HHMMSS;
        File f = new File(path);

        if (f != null && !f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }

        if (isRotate) {
            Matrix matrix = new Matrix();
            matrix.reset();
            matrix.postRotate(90);
            bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
        }

        try {
            FileOutputStream out = new FileOutputStream(f);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return updateGallery(context, bmp, takenTime_YYMMDD_HHMMSS);
    }

    @SuppressLint("InlinedApi")
    private static boolean updateGallery(Context context, Bitmap bmp, String fileNm) {
        // Insert into MediaStore.
        ContentValues values = new ContentValues();
        values.put(ImageColumns.TITLE, fileNm);
        values.put(ImageColumns.DISPLAY_NAME, fileNm);
        values.put(ImageColumns.DATE_TAKEN, System.currentTimeMillis());
        values.put(ImageColumns.MIME_TYPE, "image/jpeg");
        values.put(ImageColumns.ORIENTATION, 0);
        values.put(ImageColumns.DATA, PIC_ROOT_PATH + fileNm);
        values.put(ImageColumns.WIDTH, bmp.getWidth());
        values.put(ImageColumns.HEIGHT, bmp.getHeight());

        try {
            Uri uri = context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
            if (uri == null) {
                Log.e("storeImage", "Failed to insert MediaStore");
                return false;
            } else {
                context.sendBroadcast(new Intent("com.android.camera.NEW_PICTURE", uri));
            }
        } catch (Exception e) {
            Log.e("storeImage", "Failed to write MediaStore", e);
            return false;
        }

        return true;
    }
}