来自资源的图像缩略图

4
我想制作一个图像缩略图。该图像在资源-可绘制中。有人能帮帮我吗。

2
你想要展示它像缩略图还是将其制作成缩略图并保存? - Chirag
我想制作图像的缩略图并将其保存在SD卡中。 - Sreedev
2个回答

9

试试这段代码

     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) {

    }

0

//读取你的可绘制对象

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);
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接