Thumbnail of an image from resources

╄→尐↘猪︶ㄣ 提交于 2019-12-10 17:55:30

问题


I want to make a thumbnail of an image.The image is in the resourse-drawable.Can anyone help me.


回答1:


try this code

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

    }



回答2:


//read your drawables

InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
Bitmap           mThumbnail = scaleBitmap(BitmapFactory.decodeStream(is));

//now you can save the bitmap mThumbnail to SDcard

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


来源:https://stackoverflow.com/questions/9461283/thumbnail-of-an-image-from-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!