2014-12-03 95 views
4

我正在開發基於phonegap的應用程序,它具有文件傳輸功能。我正在使用phonegap相機插件來選擇圖像文件。該代碼適用於'DestinationType.DATA_URL'。但我無法使用'DestinationType.FILE_URI'訪問文件。問題Phonegap相機插件(DestinationType.FILE_URI)

DestinationType.DATA_URL只是給出圖像文件的內容。但我必須獲取圖像文件名稱&文件路徑及其內容。所以我必須在相機選項中使用'DestinationType.FILE_URI'。下面是我的代碼,

function attachFile() { 
var pictureSource=navigator.camera.PictureSourceType; 
var cameraOptions = { quality: 49 , destinationType: 
Camera.DestinationType.FILE_URI, sourceType: pictureSource.PHOTOLIBRARY };    
navigator.camera.getPicture(attachSuccess, attachFail, cameraOptions); 
} 

function attachSuccess(fileuri) { 
filePath = JSON.stringify(fileuri); 
console.log("FilePath: "+filePath); 
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function attachFail() { 
console.log("attach failed"); 
} 

function gotFS(fileSystem) {    

    console.log("gotFS:");  
    var root = "/"+fileSystem.root.name; 
    console.log("root: "+root); 
    filePath = filePath .substring(filePath.indexOf(root));   

    var imageName = filePath.substring(filePath.lastIndexOf('/')); 
    var type = imageName.substring(filePath.indexOf('.')); 
    fileSystem.root.getFile(filePath, null, gotFileEntry, fail); 

} 

function fail() { 
    console.log("** failed **"); 
} 

function gotFileEntry(fileEntry) { 
    console.log("got file entry"); 
    fileEntry.file(gotFile, fail); 
} 

function gotFile(file) { 
    console.log("got file");  
} 

當我稱之爲「attachFile」功能,獲取圖片窗口打開&我能夠選擇映像文件。然後,attachSuccess回調函數正在執行。但我無法使用FILE URI訪問文件。文件URI是越來越如下印刷,

內容://媒體/外部/圖像/媒體/ 5490

我不知道如何可以從這個URI獲得「文件名」或「文件對象」。請建議。 (在Android Kitkat & Lollipop中測試的代碼)

+0

「訪問文件」是什麼意思?你想獲得它的數據作爲二進制流或類似的東西? – UnTraDe 2014-12-15 05:05:51

回答

0

您可以使用Cordova的FileSystem API。下面是如何加載圖像文件的一個簡單的例子:

window.resolveLocalFileSystemURL(filePath, // The file path 
    function(fileEntry) { // found the file 
     fileEntry.file(function(f) { // got the file 
      var reader = new FileReader(); 

      reader.onloadend = function(evt) { 
       var fileContent = reader.result; 

       // Do something with the fileContent 
       someImage.attr("src", fileContent); // Example 
      } 

      reader.readAsDataURL(f); // TODO find a way to read it straight to the right format (if there is any) 
     }, function(e) { // cannot open file 
      console.log("Error opening file: " + e.message); 
     }); 
    }, 
    function(e) { // file not found 
     console.log("Error finding file: " + e.message); 
    }); 

fileContent是一個字符串包含'data:' + base64編碼字符串。 您也可以使用f變量來獲取文件名。

參考文獻:

https://developer.mozilla.org/en-US/docs/Web/API/FileReader.readAsDataURL

https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md

0

,你得到的URI是本地URI。嘗試從cameraOptions中移除destinationType字段,因爲插件默認爲FILE_URI。

如果不工作,給destinationType我猜Camera.DestinationType對象以某種方式混合出的1

值。

P.S.如果您的最終目標是上傳照片,請嘗試使用cordova-plugin-file-transfer。

0

你的代碼非常好。

您應該確認,要麼你在config.xml中或不添加文件插件的詳細信息。這可能是沒有正確選擇文件細節的原因。

<gap:plugin name="org.apache.cordova.file"/> 

和/或以下,以防萬一您想要將文件傳輸到其他地方。

<gap:plugin name="org.apache.cordova.file-transfer"/> 

this & this看看。兩者都使用destinationType.FILE_URI,但無法進一步處理,但一旦在config.xml中正確添加了插件詳細信息,它就可以正常工作。