转换为GIF PNG编程 [英] Convert GIF to PNG programatically

查看:166
本文介绍了转换为GIF PNG编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我从网站抓住一个GIF( http://radar.weather 。州长/脊/ RadarImg / N0R / )和想要显示给用户。我建立到果冻豆(4.1)和我对这个问题的搜索发现,GIF兼容性是其出路Android和平出不上果冻豆的工作。

I have a GIF that I am grabbing from a website (http://radar.weather.gov/ridge/RadarImg/N0R/) and wanting to display to the user. I am building to Jelly Bean (4.1) and in my searches on this subject found that GIF compatibility is on its way out for Android and flat out doesn't work on Jelly Bean.

所以,我想对GIF在转换为动态PNG格式。我会怎么做呢?它是那样简单从GIF中的字节读取到一个PNG文件?

So, I want to convert the GIF over to a PNG on the fly. How would I do this? Is it as simple as reading in the bytes from the GIF to a PNG file?

我将使用一个ImageView的显示UI上的形象。

I will be using an ImageView to display the image on the UI.

推荐答案

改变我的问题了一下后,我发现通过谷歌全能这里的答案是:

After changing my question a bit, I found the answer through the almighty Google here:

的http:// gayashan-a.blogspot.com/2012/02/android-how-to-display-gif-image-in.html

总结如下:

public void getBitmap(String url)
{
    Bitmap bmp = null;
    try
    {
        HttpClient client = new DefaultHttpClient();
        URI imageUri = new URI(url);
        HttpGet req = new HttpGet();
        req.setURI(imageUri);
        HttpResponse resp = client.execute(req);
        bmp = BitmapFactory.decodeStream(resp.getEntity().getContent());            
    }
    catch(URISyntaxException ex)
    {           
        Log.e("ERROR", ex.getMessage());
    }
    catch(ClientProtocolException ex)
    {
        Log.e("ERROR", ex.getMessage());
    }
    catch(IllegalStateException ex)
    {
        Log.e("ERROR", ex.getMessage());
    }
    catch(IOException ex)
    {
        Log.e("ERROR", ex.getMessage());
    }

    return bmp;
}

本德codeS到一个位图,它可以是COM pressed为PNG ...

This decodes it to a Bitmap which can be compressed to a PNG ...

ByteArrayOutputStream stream = new ByteArrayOutputStream();     
mCurrentRadar.compress(Bitmap.CompressFormat.PNG, 100, stream);

...或者可以在马上使用的ImageView ...

ImageView imageView = (ImageView) findeViewById(R.id.radarImageView);
imageView.setImageBitmap(getBitmap("http://radar.weather.gov/ridge/RadarImg/N0R/ABC_N0R_0.gif");

这篇关于转换为GIF PNG编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆