Gets bytes of image from Uri - Android Network

Android examples for Network:Uri

Description

Gets bytes of image from Uri

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;

import android.provider.MediaStore;

public class Main {
    /**/*from   ww  w. j a  v a  2  s  . c o m*/
     * Gets bytes of image from Uri
     * @param uri
     * @return
     */
    public static byte[] getBytesFromImage(Context context, Uri uri) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        // Get bitmap
        Bitmap bmp = readBitmapFromUriInPortraitMode(context, uri);
        bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream);

        // Turn it into bytes
        byte[] bytes = stream.toByteArray();

        return bytes;
    }
    public static Bitmap readBitmapFromUriInPortraitMode(Context context,
            Uri imageURI, boolean reduceSize) {
        // Get bitmap
        Bitmap bm = null;
        BitmapFactory.Options options = new BitmapFactory.Options();

        if (reduceSize)
            options.inSampleSize = 5;

        AssetFileDescriptor fileDescriptor = null;

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

        finally {
            try {
                bm = BitmapFactory.decodeFileDescriptor(
                        fileDescriptor.getFileDescriptor(), null, options);
                fileDescriptor.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // Rotate image to fix orientation
        ExifInterface exif = null;

        try {
            if (getRealPathFromUri(context, imageURI) != null)
                exif = new ExifInterface(getRealPathFromUri(context,
                        imageURI));
            else
                exif = new ExifInterface(imageURI.getPath());
        } catch (IOException e) {
            e.printStackTrace();
        }

        int exifOrientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        int rotate = 0;
        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        }

        if (rotate != 0) {
            int w = bm.getWidth();
            int h = bm.getHeight();
            Matrix mtx = new Matrix();
            mtx.preRotate(rotate);

            bm = Bitmap.createBitmap(bm, 0, 0, w, h, mtx, false);
            bm = bm.copy(Bitmap.Config.ARGB_8888, true);
        }

        return bm;
    }
    public static Bitmap readBitmapFromUriInPortraitMode(Context context,
            Uri imageURI) {
        return readBitmapFromUriInPortraitMode(context, imageURI, false);
    }
    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj,
                    null, null, null);

            if (cursor == null)
                return null;

            if (cursor.getColumnIndex(MediaStore.Images.Media.DATA) != -1) {
                int columnIndex = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(columnIndex);
            }

            return null;
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
}

Related Tutorials