比較用decodeFileDescriptor和decodeFile的區別


從本地中讀取圖片,可以用decodeFileDescriptor和decodeFile,至於哪一種方式的耗內存情況作了一次簡單對比,可能一次選取6張圖片數量過少,貌似區別不大,decodeFileDescriptor能節省1.6mb左右:

1、decodeFileDescriptor的讀取6張圖片(中間包含壓縮、讀取、轉Base64)

2、decodeFile的讀取6張圖片(中間包含壓縮、讀取、轉Base64)

 

public static Bitmap decodeSampledBitmapFromFile(String filepath,int reqWidth,int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        FileInputStream is = null;
        Bitmap bitmap = null;
        try {
            is = new FileInputStream(filepath);
            BitmapFactory.decodeFileDescriptor(is.getFD(),null, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);
        options.inJustDecodeBounds = false;

        try {
            bitmap = BitmapFactory.decodeFileDescriptor(is.getFD(),null, options);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // do nothing here
                }
            }
        }
        return bitmap;
    }
public static Bitmap decodeSampledBitmapFromFile2(String filepath,int reqWidth,int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filepath, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filepath, options);
    }

 再上50張圖片的對比:

 decodeFileDescriptor一共用了34mb

decodeFile觸發GCC自動回收內存了??

 

這里再回頭看看壓縮處理后的代碼:

for(int i=0 ; i<mSelectPath.size();i++){
            String path = mSelectPath.get(i);
            //壓縮處理(需要提示)
            Bitmap bitmap = PictureUtil.decodeSampledBitmapFromFile(path,MyConfig.PicMaxWidth,MyConfig.PicMaxHeight);
            //保存圖片到相應文件夾(質量可能變差)
    //      PictureUtil.saveBitmapWithCompress(bitmap,50,filesList);
            if(!bitmap.isRecycled() ){
                bitmap.recycle();   //回收圖片所占的內存
                System.gc();    //提醒系統及時回收
            }
        }

通過添加bitmap內存回收和釋放:

decodeFileDescriptor的使用情況如下:

這是只使用了16mb


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM