get Scale Bitmap By Uri - Android android.graphics

Android examples for android.graphics:Bitmap Operation

Description

get Scale Bitmap By Uri

Demo Code


//package com.java2s;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

public class Main {
    public static Bitmap getScaleBitmapByUri(Uri uri, Context context, int w)
            throws FileNotFoundException {
        Bitmap bitmap;//from  ww w.j a  va  2  s . c o m
        int meaW;
        InputStream is = context.getContentResolver().openInputStream(uri);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        bitmap = BitmapFactory.decodeStream(is, null, options);
        meaW = options.outWidth;
        if (meaW > w) {
            options.inSampleSize = meaW / w;
            options.inJustDecodeBounds = false;
            String path = getPicPathByUri(uri, context);
            bitmap = BitmapFactory.decodeFile(path, options);
        }
        return bitmap;
    }

    private static String getPicPathByUri(Uri uri, Context context) {
        String path = "";
        Uri imageUri = uri;
        Cursor cursor = context.getContentResolver().query(imageUri, null,
                null, null, null);
        if (cursor.moveToNext()) {
            path = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));

        }
        return path;
    }
}

Related Tutorials