【发布时间】:2012-03-16 16:55:51
【问题描述】:
我想制作一张图片的缩略图。图片在资源可绘制中。谁能帮帮我。
【问题讨论】:
-
您想像缩略图一样显示还是将其设置为缩略图并保存?
-
我要制作图片的缩略图并保存在sd卡中
标签: android image thumbnails
我想制作一张图片的缩略图。图片在资源可绘制中。谁能帮帮我。
【问题讨论】:
标签: android image thumbnails
试试这个代码
im=(ImageView)findViewById(R.id.imageView1);
byte[] imageData = null;
try
{
final int THUMBNAIL_SIZE = 64;
//InputStream is=getAssets().open("apple-android-battle.jpg");
FileInputStream fis = new FileInputStream("/sdcard/apple.jpg");
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();
im.setImageBitmap(imageBitmap);
}
catch(Exception ex) {
}
【讨论】:
//读取你的drawables
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
Bitmap mThumbnail = scaleBitmap(BitmapFactory.decodeStream(is));
//现在您可以将位图mThumbnail保存到SD卡
/*convert your image to an thumbnail view */
private static Bitmap scaleBitmap(Bitmap source) {
int maxSize = source.getWidth() > source.getHeight() ? source.getWidth() : source.getHeight();
return Bitmap.createScaledBitmap(source, source.getWidth() * 96 / maxSize, source.getHeight() * 96 / maxSize, true);
}
【讨论】: