2016-01-18 71 views
1

我正在研究需要構建自定義圖片庫選取器的科爾多瓦應用程序。我在Instagram和Twitter(如下圖)應用程序中看到過類似的東西,它們允許用戶從相機膠捲中選擇圖像,而不需要打開默認的OS提供的圖像選擇器。我搜索了各種插件,只找到了使用默認圖庫選擇器的東西。Cordova - 從設備獲取圖像陣列? (自定義圖片庫選取器)

我會假設解決這個問題的方法是以某種方式從操作系統獲取某種圖像數組,然後從中創建圖像庫選擇器,但我不確定該如何去做。

任何人有任何想法或解決方案?

謝謝!

PS。以下是iOS上的Twitter應用程序的屏幕截圖。撰寫推文時,它允許您滾動瀏覽從最近到最早排序的圖像列表,而無需打開默認的iOS圖像選擇對話框。

Custom Image Gallery in Twitter App

回答

0

您可以通過科爾多瓦文件的插件(https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/

不過我用的離子本地(科爾多瓦文件的插件的包裝)做到這一點,步驟必須相同。

import { File } from '@ionic-native/file' 

class MyFiles { 

    constructor(private file: File){ 
     this.getFiles("WhatsApp/Media") // Here I am trying to get the photos from WhatsApp application. You may provide any other folder or root folder. 
    } 


    getFiles(dir: string): void { 

     this.file.listDir(this.file.externalRootDirectory, dir).then(
      (files) => { 

      for (let file of files) { 
       if (file.isDirectory && file.name != '.' && file.name != '..') { 
       this.getFiles(dir + "/" + file.name) // recursively scan sub dirs 
       } else { 
       if (file.nativeURL.endsWith("jpg") || file.nativeURL.endsWith("jpeg") || file.nativeURL.endsWith("gif")) { 
        // Do your stuff with file 
        } 
       } 
       } 
      } 
      } 
     ).catch((err) => { 
      console.log(err.toString()) 
     }); 
}