Reads bitmap from given Uri - Android android.graphics

Android examples for android.graphics:Bitmap Load Save

Description

Reads bitmap from given Uri

Demo Code


//package com.java2s;

import java.io.FileNotFoundException;

import java.io.IOException;

import android.content.Context;
import android.content.res.AssetFileDescriptor;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

public class Main {
    /**/*from w  w  w. j  av  a 2 s  .com*/
     * Reads bitmap from given Uri
     * @param uri
     * @return
     */
    public static Bitmap readBitmapFromUri(Context context, Uri uri) {
        Bitmap bm = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        AssetFileDescriptor fileDescriptor = null;

        try {
            fileDescriptor = context.getContentResolver()
                    .openAssetFileDescriptor(uri, "r");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        finally {
            try {
                bm = BitmapFactory.decodeFileDescriptor(
                        fileDescriptor.getFileDescriptor(), null, options);
                fileDescriptor.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return bm;
    }
}

Related Tutorials