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 android.annotation.SuppressLint;

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

import android.graphics.Bitmap;

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 path */
    public static final String PIC_ROOT_PATH = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + File.separator;

    @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;
    }
}