0

I have a problem with the rotation of images.

I try:

        Matrix matrix = new Matrix();
    matrix.postRotate(90);   
    BitmapFactory.Options options=new BitmapFactory.Options();
    options.inSampleSize = 4;
    Bitmap pic = BitmapFactory.decodeFile(filePath, options);
    Bitmap rotatedPhoto = Bitmap.createBitmap(pic, 0, 0, pic.getWidth(), pic.getHeight(), matrix, true);
    photo.setImageBitmap(rotatedPhoto);

    try {
        stream = new FileOutputStream(filePath);
        rotatedPhoto.compress(CompressFormat.JPEG, 100 ,stream);
        }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }

Picture rotating, but the quality is very much lost. How do I solve this problem? How do I rotate the image without losing quality? Thank you!

Update: And how to rotate image without losing resolution?

Cocuckalol
  • 34
  • 2
  • 5

1 Answers1

1

I think your problem is arising because you are setting inSampleSize to 4. This means the returned image will be a factor of 4 smaller than the original image.

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize

Try setting options.inSampleSize to 1 - does this help?

Be careful when dealing with images though - you have very little memory to play with in an Android app. Loading just a couple of large images into memory at once can often cause your app to crash.

Jasongiss
  • 278
  • 1
  • 4
  • 11
  • I get an outOfMemory error. I tried to remove this option and save the image without displaying in imageview, but the photo quality is still lost. – Cocuckalol Feb 20 '14 at 14:11
  • This is always something you need to be wary of when manipulating images.How big is the original image? Maybe try setting inSampleSize to 2 if it's a very big image - you may need to experiment. Are you calling this in a loop? Make sure you dispose of the memory you're using properly. Otherwise, i guess your app may just be operating close to the memory limit before this point? In which case, you may need to go through the rest of your app and look for other areas which may be hogging memory. – Jasongiss Feb 20 '14 at 14:18
  • Actually, i see you do have 2 copies of the bitmap in memory at the same time (pic & rotatedPic). This could be the cause of the crash? At which point is it crashing? – Jasongiss Feb 20 '14 at 14:24
  • I decided to use "PNG". Thank you all! – Cocuckalol Feb 23 '14 at 22:19