【android】 如何把gif图片下载到本地

以上图片大家可以看到,虽然是个jpg格式的文件,但是本质上是个动图。

但是发现在咱的图片模块下,本地存储的图片只有一帧,问题出在哪里呢?

http获取到的byte[]数据是没问题的

断点跟踪了下,发现问题出现在最后一句压缩图片尺寸的时候。

public static Bitmap getScaledBitMap(byte[] data, int width, int height) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, options);

        float srcWidth = options.outWidth;
        float srcHeight = options.outHeight;
        int inSampleSize = 1;

        if (srcHeight > height || srcWidth > width) {
            if (srcWidth > srcHeight)
                inSampleSize = Math.round(srcHeight / height);
            else
                inSampleSize = Math.round(srcWidth / width);
        }

        options = new BitmapFactory.Options();
        options.inSampleSize = inSampleSize;

        return BitmapFactory.decodeByteArray(data, 0, data.length, options);
    }

最后的解决之道是,不经过Bitmap,直接把http获取到的byte[]数据写入到本地;在取出的时候,才进行图片尺寸压缩。

 /**
     * 写入bytes
     *
     * @param url
     * @param bytes
     * @return
     */
    public boolean save(String url, byte[] bytes) {
        if (bytes == null || bytes.length == 0)
            return false;

        url = trans2Local(url);
        File file = new File(url);

        if (file.exists())
            return true;

        ZIO.createNewFile(file);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(bytes);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("存储出错", e.getMessage());
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

这种做法额外的好处是,不再理会奇怪的图片格式质量问题。

比如我们用Bitmap保存图片的时候还要取判断图片类型,还要去指定压缩精度(如果100的话图片尺寸比原图还要大很多,真奇怪)

    Bitmap.CompressFormat format = url.toLowerCase().indexOf("png") > 0 ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG;
    bitmap.compress(format, 75, fos);
时间: 2024-08-10 21:27:02

【android】 如何把gif图片下载到本地的相关文章

将网页中的图片下载到本地的方法

/** * 传入要下载的图片的url列表,将url所对应的图片下载到本地 * @param urlList */ public static String downloadPicture(String urlString,String path) { URL url = null; String imgPath = null; try { url = new URL(path+urlString); // 打开URL连接 URLConnection con = url.openConnectio

scrapy框架来爬取壁纸网站并将图片下载到本地文件中

首先需要确定要爬取的内容,所以第一步就应该是要确定要爬的字段: 首先去items中确定要爬的内容 class MeizhuoItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() # 图集的标题 title = scrapy.Field() # 图片的url,需要来进行图片的抓取 url = scrapy.Field() pass 在确定完要爬的字段之后,就是分析网站页面的请求

android开源项目:图片下载缓存库picasso

picasso是Square公司开源的一个Android图形缓存库,地址http://square.github.io/picasso/,可以实现图片下载和缓存功能. picasso有如下特性: 在adapter中回收和取消当前的下载: 使用最少的内存完成复杂的图形转换操作: 自动的内存和硬盘缓存: 图形转换操作,如变换大小,旋转等,提供了接口来让用户可以自定义转换操作: 加载载网络或本地资源: 可以转换为自己需要的request(Square公司开源的另一个网络支持库:retrofit支持转化

svg保存为图片下载到本地

今天给大家说一个将svg下载到本地图片的方法,这里我不得不吐槽一下,为啥博客园不可以直接上传本地文件给大家用来直接下载分享呢,好,吐槽到此为止! 这里需要用到一个js文件,名字自己起,内容如下: (function() { const out$ = typeof exports != 'undefined' && exports || typeof define != 'undefined' && {} || this || window; if (typeof defin

Android图片下载到本地,系统图库不显示

可能大家都知道我们下载图片到Android手机的时候,然后调用系统图库打开图片,提示"找不到指定项". 那是因为我们插入的图片还没有更新的缘故,所以只要将图片插入系统图库,之后发条广播就ok了. /** * 图片插入到系统相册,解决系统图库不能打开图片的问题 */ public static void insertImageToSystemGallery(Context context, String filePath, Bitmap bitmap){ MediaStore.Image

用thinkphp将网络上的图片下载到本地服务器

我用的thinkphp版本是3.2.3,这个版本的跟更早些版本的调用方法不太一样,正确的调用方法是: Demo3Controller.class <?php namespace Home\Controller; use Think\Controller; class Demo3Controller extends Controller { public function download(){ $url = "http://n.sinaimg.cn/sports/20161023/MrD2

抓取服务器图片下载到本地

Sample code: import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods

将乌云漏洞图片下载到本地

#coding=utf-8 import MySQLdb import re import requests conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='123456',db='wooyunbugs',charset='utf8') cursor = conn.cursor() def q(): sql = 'select * from `a`' cursor.execute(sql) for row in cursor

通过URl将服务器的图片下载到本地并压缩

private void downloadServerPic(final String url1) { new Thread() { @Override public void run() { // 定义一个URL对象 URL url; try { url = new URL(url1); // 打开该URL的资源输入流 InputStream is = url.openStream(); is.close(); // 再次打开RL对应的资源输入流 is = url.openStream();