Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.FileNotFoundException;

import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

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

import android.net.Uri;

public class Main {
    /**
     * Returns a bitmap from a gallery Uri
     *
     * @param pContext
     *        Context required to access the content resolver
     * @param pIntent
     *        The Uri of the picker image
     * @return The picked image as a bitmap
     */
    public static Bitmap getBitmapFromIntent(Context pContext, Intent pIntent) {
        Bitmap bitmapPickedImage = null;

        Uri pickedImageUri = pIntent.getData();

        // If the URI is not null try to decode it to a bitmap else try to get the bitmap data from the intent
        // http://stackoverflow.com/questions/17123083/null-pointer-exception-while-taking-pictures-from-camera-android-htc
        if (pickedImageUri != null) {
            try {
                InputStream imageStream = pContext.getContentResolver().openInputStream(pickedImageUri);
                bitmapPickedImage = BitmapFactory.decodeStream(imageStream);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            if (pIntent.getExtras() != null && pIntent.getExtras().get("data") instanceof Bitmap) {
                bitmapPickedImage = (Bitmap) pIntent.getExtras().get("data");
            }
        }

        return bitmapPickedImage;
    }
}