Code example for Context

Methods: getContentResolver

0
            return totalBytesSkipped;
        } 
    } 
 
    public static byte[] scaleImage(Context context, Uri photoUri) throws IOException {
        InputStream is = context.getContentResolver().openInputStream(photoUri);
        BitmapFactory.Options dbo = new BitmapFactory.Options();
        dbo.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, dbo);
        is.close();
 
        int rotatedWidth, rotatedHeight;
        int orientation = getOrientation(context, photoUri);
 
        if (orientation == 90 || orientation == 270) {
            rotatedWidth = dbo.outHeight;
            rotatedHeight = dbo.outWidth;
        } else { 
            rotatedWidth = dbo.outWidth;
            rotatedHeight = dbo.outHeight;
        } 
 
        Bitmap srcBitmap;
        is = context.getContentResolver().openInputStream(photoUri);
        if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
            float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
            float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
            float maxRatio = Math.max(widthRatio, heightRatio);
 
            // Create the bitmap from file 
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = (int) maxRatio;
            srcBitmap = BitmapFactory.decodeStream(is, null, options);
        } else { 
            srcBitmap = BitmapFactory.decodeStream(is);
        } 
        is.close();
 
        /* 
         * if the orientation is not 0 (or -1, which means we don't know), we 
         * have to do a rotation. 
         */ 
        if (orientation > 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);
 
            srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                    srcBitmap.getHeight(), matrix, true);
        } 
 
        String type = context.getContentResolver().getType(photoUri);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (type.equals("image/png")) {
            srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
            srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        } 
        byte[] bMapArray = baos.toByteArray();
        baos.close();
        return bMapArray;
    } 
 
    public static int getOrientation(Context context, Uri photoUri) {
        /* it's on the external media. */ 
        Cursor cursor = context.getContentResolver().query(photoUri,
                new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
 
        if (cursor.getCount() != 1) {
            return -1; 
        }