Android——加载gif图片的问题

  最近在做一个项目,遇到一个问题,想在这里请教下大神,如何在android6.0的基础上加载gif图片呢?如果大神看到了,麻烦在评论里面赐教下,也让大伙学习学习,先谢过。如果我找到解决办法了也会分享出来。
  
  下面是我写的一个加载gif图片的源码,直接放在onCreate方法里面就行了,只能支持5.0以前的版本,异步加载也太老了点。说一下需要注意的文件与步骤:

new AsyncTask<Void, Void, byte[]>() {
            protected byte[] doInBackground(Void... params) {
                String IMAGE_URL = "http://image.sinajs.cn/newchart/min/n/sz300066.gif";
                byte[] gifbyte = null;
                ByteArrayOutputStream out = null;
                InputStream in = null;
                HttpURLConnection conn = null;
                try {
                    URL url = new URL(IMAGE_URL);
                    conn = (HttpURLConnection) url.openConnection();
                    out = new ByteArrayOutputStream();
                    in = conn.getInputStream();
                    if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                        return null;
                    }
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = in.read(buffer)) > 0) {
                        out.write(buffer, 0, len);
                    }
                    gifbyte = out.toByteArray();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        in.close();
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                        conn.disconnect();
                }
                return gifbyte;
            }

            protected void onPostExecute(byte[] gifbyte) {
                Movie gif = Movie.decodeByteArray(gifbyte, 0, gifbyte.length);
                if (gif != null) {
                    GifDrawable gifDrawable = null;
                    try {
                        gifDrawable = new GifDrawable(gifbyte);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    myGifImageView.setImageDrawable(gifDrawable);
                } else {
                    Bitmap gifBitmap = BitmapFactory.decodeByteArray(gifbyte, 0, gifbyte.length);
                    myGifImageView.setImageBitmap(gifBitmap);
                }
            }

            ;
        }.execute();

以下是需要的jni文件,这里我就贴出图片来了,后面给大家一个下载地址。
这里写图片描述

这里需要注意的是:android studio新建jni文件夹要修改为jniLibs,这个是固定的,不可命名为别的,否则加载不到.so文件。

jniLibs下载地址

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值