本文内容

  • 环境
  • 演示

下载 Demo

环境


  • Windows 2008 R2 64 位
  • Eclipse ADT V22.6.2,Android 4.4.3
  • SAMSUNG GT-I9008L,Android OS 2.2.2

演示


缩略图能减少手机内存的消耗。网络上的资源一般对手机来说,大了点,把图片解码,减少其尺寸,从而减少手机内存的消耗。

本文采用的歌曲列表是 Android_Music_Demo_json_h_array.xml 文件,虽然文件后缀名是 .xml,但内部其实是 JSON 格式,因为 cnblog 不让上传 .json 格式的文件,该文件里的缩略图都较大,是 .jpg 格式,而与之对应的 Android_Music_Demo_json_array.xml 文件里的缩略图,都很小,都是才不到10k的 .png 格式。

图 1

假设给定一个图片链接地址,想获得图片缩略图,核心代码如下所示,点击此处下载:

private Bitmap getBitmap(String url) {
        File f = fileCache.getFile(url);
    
        // from SD cache
        Bitmap b = decodeFile(f);
        if (b != null)
            return b;
    
        // from web
        try {
            Bitmap bitmap = null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imageUrl
                    .openConnection();
    
            conn.setRequestMethod("GET");
            // Sets the flag indicating whether this URLConnection allows input.
            // conn.setDoInput(true);
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            // Flag to define whether the protocol will automatically follow
            // redirects or not.
            conn.setInstanceFollowRedirects(true);
            int response_code = conn.getResponseCode();
            if (response_code == 200) {
                InputStream is = conn.getInputStream();
                OutputStream os = new FileOutputStream(f);
                StreamUtils.CopyStream(is, os);
                os.close();
                conn.disconnect();
                bitmap = decodeFile(f);
                return bitmap;
            } else {
                conn.disconnect();
                return null;
            }
    
        } catch (Throwable ex) {
            ex.printStackTrace();
            if (ex instanceof OutOfMemoryError)
                memoryCache.clear();
            return null;
        }
    }
private Bitmap decodeFile(File f) {
        try {
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            FileInputStream stream1 = new FileInputStream(f);
            BitmapFactory.decodeStream(stream1, null, o);
            stream1.close();
    
            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }
    
            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            FileInputStream stream2 = new FileInputStream(f);
            Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
            stream2.close();
            return bitmap;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

下载 Demo

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. Pro Android(安卓)4 第三章 理解Android中的资源
  4. Android高手进阶教程(七)之----Android(安卓)中Preferences的使
  5. Android2.2快速入门
  6. Android修改icon和roundIcon后,显示的图标还是默认的图片
  7. 用python开发android应用(1)
  8. Android(安卓)打开本地pdf文件,android 加载pdf文件
  9. Android(安卓)Manifest文件

随机推荐

  1. Android为图片添加水印文字
  2. Android Exception异常汇集【不定时更新
  3. android直接读取数据库文件
  4. Android各种屏幕尺寸
  5. Android 相机或者相册上传头像
  6. Android System Bring Up
  7. Android使用wifi Ap核心类
  8. 【Android】loadData与loadDataWithBaseU
  9. Android 控件之八:AutoCompleteTextView自
  10. Building Android notifications 2.3 thr