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 android.content.Context;
import android.content.Intent;

import android.net.Uri;
import android.os.SystemClock;
import android.provider.MediaStore;

import android.text.TextUtils;

public class Main {
    private static final String PNG = ".png";

    public static boolean insertMediaStore(Context context, File file, String imageName) {
        if (context == null || file == null || !file.exists()) {
            return false;
        }

        if (TextUtils.isEmpty(imageName)) {
            imageName = SystemClock.currentThreadTimeMillis() + PNG;
        }
        try {

            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), imageName,
                    null);
            // String imagePath =
            // MediaStore.Images.Media.insertImage(getContentResolver(),
            // bitmap, "", "");

            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }

        return true;

    }
}