码迷,mamicode.com
首页 > 其他好文 > 详细

decodeFileDescriptor()与bimap比decodeFile()比较

时间:2014-08-27 16:05:28      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   java   io   ar   div   

要点:
1、用decodeFileDescriptor()来生成bimap比decodeFile()省内存,将

Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);
imageView.setImageBitmap(bmp);

替换为

1 FileInputStream is = = new FileInputStream(path);
2 bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);

 

原因:
查看BitmapFactory的源码,对比一下两者的实现,可以发现decodeFile()最终是以流的方式生成bitmap 

decodeFile源码:

 

 

 1 public static Bitmap decodeFile(String pathName, Options opts) {
 2     Bitmap bm = null;
 3     InputStream stream = null;
 4     try {
 5         stream = new FileInputStream(pathName);
 6         bm = decodeStream(stream, null, opts);
 7     } catch (Exception e) {
 8         /*  do nothing.
 9             If the exception happened on open, bm will be null.
10         */
11     } finally {
12         if (stream != null) {
13             try {
14                 stream.close();
15             } catch (IOException e) {
16                 // do nothing here
17             }
18         }
19     }
20     return bm;
21 }

 

decodeFileDescriptor的源码,可以找到native本地方法decodeFileDescriptor,通过底层生成bitmap

decodeFileDescriptor源码:

 1 public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts) {
 2        if (nativeIsSeekable(fd)) {
 3            Bitmap bm = nativeDecodeFileDescriptor(fd, outPadding, opts);
 4            if (bm == null && opts != null && opts.inBitmap != null) {
 5                throw new IllegalArgumentException("Problem decoding into existing bitmap");
 6            }
 7            return finishDecode(bm, outPadding, opts);
 8        } else {
 9            FileInputStream fis = new FileInputStream(fd);
10            try {
11                return decodeStream(fis, outPadding, opts);
12            } finally {
13                try {
14                    fis.close();
15                } catch (Throwable t) {/* ignore */}
16            }
17        }
18    }
19  
20 private static native Bitmap nativeDecodeFileDescriptor(FileDescriptor fd,Rect padding, Options opts);

具体的牵扯到底层虚拟机,目前我也给不了合理解释,各位朋友有好的解释留言

 

decodeFileDescriptor()与bimap比decodeFile()比较

标签:des   style   blog   color   os   java   io   ar   div   

原文地址:http://www.cnblogs.com/lyc602/p/3939442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!