Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.ContentResolver;
import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.provider.MediaStore;

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static Bitmap bitmapResult = null;

    public static Uri getImageUrlWithAuthority(Context context, Uri uri) {
        InputStream is = null;

        try {

            if (uri.getAuthority() == null) {
                return null;
            } else if (uri.getAuthority() != null) {
                try {
                    is = context.getContentResolver().openInputStream(uri);
                    Bitmap bmp = BitmapFactory.decodeStream(is);

                    bitmapResult = bmp;
                    return writeToTempImageAndGetPathUri(context, bmp);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * Return an {@link InputStream} from the given uri. ( can be a local
     * content, a file path or an http url )
     *
     * @param context
     * @param uri
     * @return the {@link InputStream} from the given uri, null if uri cannot be
     *         opened
     */
    public static InputStream openInputStream(Context context, Uri uri) {
        if (null == uri)
            return null;
        final String scheme = uri.getScheme();
        InputStream stream = null;
        if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) {
            // from file
            stream = openFileInputStream(uri.getPath());
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            // from content
            stream = openContentInputStream(context, uri);
        }
        return stream;
    }

    public static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

    /**
     * Return a {@link FileInputStream} from the given path or null if file not
     * found
     *
     * @param path
     *            the file path
     * @return the {@link FileInputStream} of the given path, null if
     *         {@link FileNotFoundException} is thrown
     */
    static InputStream openFileInputStream(String path) {
        try {
            return new FileInputStream(path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Return a {@link BufferedInputStream} from the given uri or null if an
     * exception is thrown
     *
     * @param context
     * @param uri
     * @return the {@link InputStream} of the given path. null if file is not
     *         found
     */
    static InputStream openContentInputStream(Context context, Uri uri) {
        try {
            return context.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}