Example usage for android.content.res Resources openRawResource

List of usage examples for android.content.res Resources openRawResource

Introduction

In this page you can find the example usage for android.content.res Resources openRawResource.

Prototype

@NonNull
public InputStream openRawResource(@RawRes int id, TypedValue value) throws NotFoundException 

Source Link

Document

Open a data stream for reading a raw resource.

Usage

From source file:Main.java

public static Bitmap decodeResource(Resources res, int resId, int reqWidth, int reqHeight) {
    BitmapFactory.Options opts = new BitmapFactory.Options();

    // Determine insample size
    opts.inJustDecodeBounds = true;// w  w w.  j a v  a2  s .c o  m
    BitmapFactory.decodeResource(res, resId, opts);
    opts.inSampleSize = calculateInSampleSize(opts, reqWidth, reqHeight);

    // Decode the bitmap, regionally if necessary
    Bitmap bitmap = null;
    opts.inJustDecodeBounds = false;
    Rect rect = getCropRectIfNecessary(opts, reqWidth, reqHeight);

    InputStream stream = null;
    try {
        if (rect != null) {
            stream = res.openRawResource(resId, new TypedValue());
            if (stream == null)
                return null;
            BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(stream, false);
            bitmap = decoder.decodeRegion(rect, opts);
        } else {
            bitmap = BitmapFactory.decodeResource(res, resId, opts);
        }
    } catch (IOException e) {
        Log.e(TAG, "Unable to open resource " + resId, e);
    } finally {
        closeSilently(stream);
    }
    return bitmap;
}