Rotate a bitmap using render script android

When I use following code, it ends up with outofmemory exception. After doing researh Render script looks like a good candidate. Where can I find sample code for similar operation and how can integrate it to my project.

public Bitmap rotateBitmap(Bitmap image, int angle) {
    if (image != null) {

        Matrix matrix = new Matrix();
        matrix.postRotate(angle, (image.getWidth()) / 2,
                (image.getHeight()) / 2);

        return Bitmap.createBitmap(image, 0, 0, image.getWidth(),
                image.getHeight(), matrix, true);
    return null;

  • Firebase: How to keep an Android user logged in?
  • best OCR (Optical character recognition) example in android
  • Immersive mode navigation becomes sticky after volume press or minimise-restore
  • Android TV: unfocused image contains black transparent background?
  • IntelliJ/Android -> “java: constant expression required” on case
  • Paginating text in Android
  • Related posts:

    How to make my app a device owner?
    Retrofit logging interceptor exception
    android: zxing barcode scan successful but not returning from activity
    How to change android version and code version number in Android Studio?
    Android: how to play music at maximum possible volume?
    Theme Google Maps for Android
  • Android N Java8 java.time
  • How to clear the views which are held in the ListView's RecycleBin?
  • How to convert .jar or .class to .dex file?
  • Android dialog width
  • Android application stats on Android market place net users/installed
  • Uncheck all RadioButton in a RadioButtonGroup
  • 2 Solutions collect form web for “Rotate a bitmap using render script android”

    Basically rotating bitmap is a task of rotating 2D array without using additional memory. And this is the correct implementation with RenderScript: Android: rotate image without loading it to memory .

    But this is not necessary if all you want is just to display rotated Bitmap. You can simply extend ImageView and rotate the Canvas while drawing on it:;
    canvas.rotate(angle, X + (imageW / 2), Y + (imageH / 2));
    canvas.drawBitmap(imageBmp, X, Y, null);

    What about ScriptIntrinsic, since it’s just a built-in RenderScript kernels for common operations you cannot do nothing above the already implemented functions: ScriptIntrinsic3DLUT, ScriptIntrinsicBLAS, ScriptIntrinsicBlend, ScriptIntrinsicBlur, ScriptIntrinsicColorMatrix, ScriptIntrinsicConvolve3x3, ScriptIntrinsicConvolve5x5, ScriptIntrinsicHistogram, ScriptIntrinsicLUT, ScriptIntrinsicResize, ScriptIntrinsicYuvToRGB. They do not include functionality to rotate bitmap at the moment so you should create your own ScriptC script.

    Try this code..

    private Bitmap RotateImage(Bitmap _bitmap, int angle) {
        Matrix matrix = new Matrix();
        _bitmap = Bitmap.createBitmap(_bitmap, 0, 0, _bitmap.getWidth(), _bitmap.getHeight(), matrix, true);
        return _bitmap;

    Use this code when select image from gallery.

    like this..

         File _file = new File(file_name);
         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inSampleSize = 1;
         Bitmap bitmap = BitmapFactory.decodeFile(file_name, options);
         try {
                ExifInterface exif = new ExifInterface(file_name);
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                    bitmap = RotateImage(bitmap, 90);
                } else if (orientation ==ExifInterface.ORIENTATION_ROTATE_270) {
                    bitmap = RotateImage(bitmap, 270);
            } catch (Exception e) {
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.