therahulsubedi
therahulsubedi

Reputation: 36

Android app crashes when rotated twice in image view for high quality images from gallery

  1. There is no problem with low quality images i.e. less than 300 KB
  2. There is no problem with single +90, 180 or 270 degrees of rotation.
  3. Only if high quality images are rotated in image view for more than two times app crashes

    private void setupListeners() {
    
    rotateLeft.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            *imageBitmap = Util.ImageProcess.rotateImage(imageBitmap, 270);*
    
            showImage.setImageBitmap(imageBitmap);
    
        }
    });
    
    rotateRight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            imageBitmap = Util.ImageProcess.rotateImage(imageBitmap, 90);
    
            showImage.setImageBitmap(imageBitmap);
    
        }
    });
    
    flip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            imageBitmap = Util.ImageProcess.rotateImage(imageBitmap, 180);
    
            showImage.setImageBitmap(imageBitmap);
    
        }
    });
    
    continueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            saveRotatedImage();
    
            Util.MediaScanner.galleryAddPic(getApplicationContext(),imageFile.getAbsolutePath());
            startNextActivity();
        }
    });
    

    }

Also, for rotation :

public static class ImageProcess{

    public static Bitmap rotateImage(Bitmap source, float angle) {

        Bitmap bitmap = null;
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        try {
            bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
        } catch (OutOfMemoryError err) {
            err.printStackTrace();
        }
        return bitmap;

Upvotes: 0

Views: 240

Answers (2)

therahulsubedi
therahulsubedi

Reputation: 36

I finally got the solution. And the code is :

   int imageHeight = imageBitmap.getHeight();

    int imageWidth = imageBitmap.getWidth();

    int newImageheight;

    int newImageWidth;

    if (imageHeight <= imageWidth){

        newImageWidth = 1200;
        //getting the length and setting it 1200

        newImageheight = (1200*imageWidth)/imageHeight;
        //getting image ratio and multiplying it by the length which
        //gives width              

    }

    else {
        newImageheight = 1200;

        newImageWidth = (1200*imageHeight)/imageWidth;

    }

   imageBitmap = Bitmap.createScaledBitmap(imageBitmap,
        newImageheight, newImageWidth, false);
    //setting the new scaled image in lower quality

This prevents the OutOfMemoryError .

Upvotes: 0

Vivek Khare
Vivek Khare

Reputation: 162

Add a property in your manifest file in the application tag to allow largeheap=true that might help give it a try.

Upvotes: 1

Related Questions