Loads given image asset, scaling the image down if it is too big to improve performance. - Android Graphics

Android examples for Graphics:Image Download

Description

Loads given image asset, scaling the image down if it is too big to improve performance.

Demo Code

/**********************************************************************
 * Copyright (c) 2015 Luka Kunic, "ActivityUtils.java"
 * //from w w w.  j a v  a  2s  .  c  om
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
 * the specific language governing permissions and limitations under the License.
 *
 * Author: lkunic
 * Last modified: 08/02/2015
 **********************************************************************/
//package com.java2s;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    private static final int THUMBNAIL_SIZE = 1024;

    /**
     * Loads given image asset, scaling the image down if it is too big to improve performance.
     * @param context Application context
     * @param path Path in the assets folder of the image to load
     * @return Loaded image bitmap
     */
    public static Bitmap loadImageFromAssets(Context context, String path) {
        try {
            // Open the input stream to the image in assets
            InputStream is = context.getAssets().open(path);

            // Load the image dimensions first so that big images can be scaled down (improves memory usage)
            BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
            onlyBoundsOptions.inJustDecodeBounds = true;
            onlyBoundsOptions.inDither = true;
            BitmapFactory.decodeStream(is, null, onlyBoundsOptions);
            is.close();

            if ((onlyBoundsOptions.outWidth == -1)
                    || (onlyBoundsOptions.outHeight == -1)) {
                // There was an error while decoding
                return null;
            }

            // Find the bigger dimension (width, height)
            int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
                    : onlyBoundsOptions.outWidth;

            // Calculate the sampling ratio for images that are bigger than the thumbnail size
            double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE)
                    : 1.0;
            int sampleSize = Integer.highestOneBit((int) Math.floor(ratio));

            // Load the image sampled using the calculated ratio
            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            bitmapOptions.inSampleSize = sampleSize;
            bitmapOptions.inDither = true;
            is = context.getAssets().open(path);
            Bitmap bitmap = BitmapFactory.decodeStream(is, null,
                    bitmapOptions);
            is.close();

            return bitmap;
        } catch (IOException e) {
            return null;
        }
    }
}

Related Tutorials