【发布时间】:2012-12-16 02:50:39
【问题描述】:
我需要图片的缩略图。我只知道存储在 SD 卡中的图像名称。谁能帮帮我。
【问题讨论】:
-
你想用那个缩略图做什么
-
我需要缩略图,因为在将所有图像 URI 直接放入 GridView 时,我不断收到
java.lang.OutOfMemoryError。
标签: android image thumbnails
我需要图片的缩略图。我只知道存储在 SD 卡中的图像名称。谁能帮帮我。
【问题讨论】:
java.lang.OutOfMemoryError。
标签: android image thumbnails
试试这个。
final int THUMBSIZE = 64;
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath),
THUMBSIZE, THUMBSIZE);
Refer this 了解更多详情。
【讨论】:
imagePath 是什么?
使用MediaStore.Images.Thumbnails可以查询得到两种缩略图:MINI_KIND: 512 x 384 thumbnail MICRO_KIND: 96 x 96 thumbnail。
使用此调用的优点是缩略图由 MediaStore 缓存。因此,如果之前创建了缩略图,检索会更快。
【讨论】:
byte[] imageData = null;
try
{
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(fileName);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
Float width = new Float(imageBitmap.getWidth());
Float height = new Float(imageBitmap.getHeight());
Float ratio = width/height;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int)(THUMBNAIL_SIZE * ratio), THUMBNAIL_SIZE, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
}
catch(Exception ex) {
}
【讨论】:
如果您喜欢总部缩略图,请使用 [RapidDecoder][1] 库。很简单,如下:
import rapid.decoder.BitmapDecoder;
...
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image)
.scale(width, height)
.useBuiltInDecoder(true)
.decode();
如果您想缩小不到 50% 并获得 HQ 结果,请不要忘记使用内置解码器。
【讨论】: