Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.graphics.Bitmap;

import android.net.Uri;

import android.provider.MediaStore;

import android.util.DisplayMetrics;
import java.io.IOException;

public class Main {
    /**
     * Get bitmap from a URI.
     *
     * @param context The context.
     * @param uriString The URI as a string.
     *
     * @return The bitmap.
     */
    public static Bitmap getBitmapFromUri(final Context context, String uriString) {
        Bitmap bitmap = null;
        if (uriString == null) {
            return null;
        }

        Uri uri = Uri.parse(uriString);
        if (uri != null) {
            try {
                bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
                if (bitmap != null) {
                    // We use default density for all bitmaps to avoid scaling.
                    bitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT);
                }
            } catch (IOException e) {

            }
        }
        return bitmap;
    }
}