Android - how to compress or downsize an image?

前端 未结 3 986
攒了一身酷
攒了一身酷 2021-01-14 01:03
ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
avatarButton.setImageResource(R.drawable.avatar);
strAvatarFilename =  \"Image.jpg\";         


        
相关标签:
3条回答
  • 2021-01-14 01:14

    You can mess with the ImageButton's scaleType properties (to fitXY for example, or cropCenter), and by setting a fixed size to LayoutPreferences (height and width) you'll be all set.

    0 讨论(0)
  • 2021-01-14 01:19
    Bitmap thumb = Bitmap.createScaledBitmap (BitmapFactory.decodeFile(photoPath), 96, 96, false);
    

    Where the second and third parameters to createScaledBitmap are the width and height respectively.

    http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29

    EDIT: It now occurred to me that you can do this in a faster, more efficient way, using BitmapFactory.Options and the inSampleSize option:

    BitmapFactory.Options opts = new BitmapFactory.Options ();
    opts.inSampleSize = 2;   // for 1/2 the image to be loaded
    Bitmap thumb = Bitmap.createScaledBitmap (BitmapFactory.decodeFile(photoPath, opts), 96, 96, false);
    

    This way you will save even more memory and the downsizing should take less time.

    0 讨论(0)
  • 2021-01-14 01:28

    The Android Library SiliCompressor provide this functionality for you to compress your images while keeping the image quality.

    0 讨论(0)
提交回复
热议问题