2017-08-03 62 views
3
ArrayList<String> imageFileList = new ArrayList<>(); 
ArrayList<RecentImagesModel> fileInfo = new ArrayList<>(); 

File targetDirector = new File(/storage/emulated/0/DCIM/Camera/); 
if (targetDirector.listFiles() != null) { 
    File[] files = targetDirector.listFiles(); 
    int i = files.length - 1; 
    while (imageFileList.size() < 10) { 
     File file = files[i]; 
     if (file.getAbsoluteFile().toString().trim().endsWith(".jpg")) { 
      imageFileList.add(file.getAbsolutePath()); 
     } else if (file.getAbsoluteFile().toString().trim().endsWith(".png")) { 
      imageFileList.add(file.getAbsolutePath()); 
     } 
     i--; 
    } 
} 

String file, filename; 
Bitmap rawBmp, proBmp; 
int length = imageFileList.size(); 

for (int i = 0; i < length; i++) { 
    RecentImagesModel rim = new RecentImagesModel(); 
    file = imageFileList.get(i); 
    rim.setFilepath(file); 
    filename = file.substring(file.lastIndexOf("/") + 1); 
    rim.setName(filename); 
    rawBmp = BitmapFactory.decodeFile(file); 
    proBmp = Bitmap.createScaledBitmap(rawBmp, rawBmp.getWidth()/6, rawBmp.getHeight()/6, false); 
    rim.setBitmap(proBmp); 
    fileInfo.add(rim); 
} 

當我轉換文件對象,位圖和重新調整它們:如何快速將圖像文件路徑列表轉換爲位圖列表?

rawBmp = BitmapFactory.decodeFile(file); 
proBmp = Bitmap.createScaledBitmap(rawBmp, rawBmp.getWidth()/6, rawBmp.getHeight()/6, false); 

它發生在處理大量的時間。有沒有辦法縮短這個過程?

回答

0

有沒有辦法縮短進程?

用你目前的算法,有一個較小的圖像列表。

你可以節省大量的時間和大量的內存:從任「原始圖像的1/4」或「原始圖像的1/6日」

  • 開關「原始圖像的」 1/8,則

  • 使用,需要一個BitmapFactory.Options,並提供一個inSampleSize的兩參數decodeFile() 2(1 /第4次)或3(1/8)

一般來說,一次加載大量圖像並不是一個好主意,所以我強烈建議您找到一種方法來加載圖像,當且僅當它們需要時。例如,如果用戶在該目錄中有數百張高分辨率照片,則會因OutOfMemoryError而崩潰,因爲您沒有足夠的堆空間來容納數百張圖像。

+0

我需要提供至少10張圖片,不能比這更小。 –

+0

嘗試了原始圖像的1/8,它將圖像快速地綁定到recylerview。但將文件對象轉換爲位圖並將它們存儲在數組列表中仍然需要時間。 –

+0

@ShubhanshJaiswal:如果您使用'RecyclerView',請使用圖像加載庫(Glide,Picasso等)根據需要加載圖像*,而不是預先加載它們。 – CommonsWare

相關問題