Android如何创建运行时缩略图

d-m*_*man 65 android

我有一个大尺寸的图像.在运行时,我想从存储中读取图像并对其进行缩放,以便减轻其重量和大小,并将其用作缩略图.当用户点击缩略图时,我想显示完整尺寸的图像.

Dax*_*Dax 130

试试这个

Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
Run Code Online (Sandbox Code Playgroud)

此实用程序可从API_LEVEl 8获得.[来源]

  • 正确的方法是解码文件的下采样版本.其他答案提供了这种方法.官方文档也很好地解释了这个过程:https://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html.由于这是一个投票最多的答案(由于其简单性),我只是想提醒大家,这不是最好的方式. (7认同)
  • 使用此代码实际上是在内存中加载大位图的副本,因此这不是管理大图像的好方法. (3认同)
  • 此解决方案是否保持纵横比? (2认同)

kak*_*ppa 46

我的解决方案

byte[] imageData = null;

        try     
        {

            final int THUMBNAIL_SIZE = 64;

            FileInputStream fis = new FileInputStream(fileName);
            Bitmap imageBitmap = BitmapFactory.decodeStream(fis);

            imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            imageData = baos.toByteArray();

        }
        catch(Exception ex) {

        }
Run Code Online (Sandbox Code Playgroud)

  • 最好使用Android的`ThumbnailUils`类,如下面的答案. (6认同)
  • @afollestad不是,这种方法是正确的。仅当您100%确定要使用小文件时,才使用ThumbnailUtils。 (2认同)

Mas*_*imo 14

我找到的最佳解决方案如下.与其他解决方案相比,这个解决方案不需要加载完整的图像来创建缩略图,因此效率更高! 它的限制是你不能有一个具有确切宽度和高度的缩略图,但解决方案尽可能接近.

File file = ...; // the image file

Options bitmapOptions = new Options();
bitmapOptions.inJustDecodeBounds = true; // obtain the size of the image, without loading it in memory
BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);

// find the best scaling factor for the desired dimensions
int desiredWidth = 400;
int desiredHeight = 300;
float widthScale = (float)bitmapOptions.outWidth/desiredWidth;
float heightScale = (float)bitmapOptions.outHeight/desiredHeight;
float scale = Math.min(widthScale, heightScale);

int sampleSize = 1;
while (sampleSize < scale) {
    sampleSize *= 2;
}
bitmapOptions.inSampleSize = sampleSize; // this value must be a power of 2,
                                         // this is why you can not have an image scaled as you would like
bitmapOptions.inJustDecodeBounds = false; // now we want to load the image

// Let's load just the part of the image necessary for creating the thumbnail, not the whole image
Bitmap thumbnail = BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions);

// Save the thumbnail
File thumbnailFile = ...;
FileOutputStream fos = new FileOutputStream(thumbnailFile);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();

// Use the thumbail on an ImageView or recycle it!
thumbnail.recycle();
Run Code Online (Sandbox Code Playgroud)

  • 这对于低内存设备最有用 (2认同)

cda*_*ung 9

这是一个将Bitmap缩小为缩略图大小的更完整的解决方案.它通过保持图像的纵横比并将它们填充到相同的宽度来扩展Bitmap.createScaledBitmap解决方案,以便它们在ListView中看起来很好.

此外,最好进行一次缩放,并将生成的Bitmap作为blob存储在Sqlite数据库中.我已经包含了一个关于如何将Bitmap转换为字节数组的代码片段.

public static final int THUMBNAIL_HEIGHT = 48;
public static final int THUMBNAIL_WIDTH = 66;

imageBitmap = BitmapFactory.decodeByteArray(mImageData, 0, mImageData.length);
Float width  = new Float(imageBitmap.getWidth());
Float height = new Float(imageBitmap.getHeight());
Float ratio = width/height;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int)(THUMBNAIL_HEIGHT*ratio), THUMBNAIL_HEIGHT, false);

int padding = (THUMBNAIL_WIDTH - imageBitmap.getWidth())/2;
imageView.setPadding(padding, 0, padding, 0);
imageView.setImageBitmap(imageBitmap);



ByteArrayOutputStream baos = new ByteArrayOutputStream();  
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] byteArray = baos.toByteArray();
Run Code Online (Sandbox Code Playgroud)


Jim*_*ler 6

使用BitmapFactory.decodeFile(...)让您的Bitmap对象,并将它设置为ImageViewImageView.setImageBitmap().

ImageView布局尺寸设置为小的,例如:

android:layout_width="66dip" android:layout_height="48dip"
Run Code Online (Sandbox Code Playgroud)

一个添加onClickListenerImageView并推出新的活动,在那里你在全尺寸显示图像

android:layout_width="wrap_content" android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)

或指定一些更大的尺寸.

  • 有多个图像时,您应该考虑事先将其缩小到拇指大小.否则可能会在移动集合时降低性能. (11认同)
  • 的确,要做到这一点,你会使用Bitmap.createScaledBitmap(originalBitmap,newWidth,newHeight,false); (5认同)