2016-08-05 28 views
9

我正在託管我的網頁,用於phonegap構建應用程序。 我想用相機上傳照片,並顯示預覽圖像,基本上是這樣:「不允許加載本地資源」從PhoneGap中的遠程頁面的本地圖像構建

<img src="file:///storage/emulated/0/Android/data/com.myapp.myapp/cache/1470418568917.jpg" height="500" /> 

因爲我的網頁託管我得到這個錯誤:

不允許加載本地資源:file:///storage/emulated/0/Android/data/com.myapp.myapp/cache/1470418568917.jpg「,來源:https://www.myapp.com/v5.5/imager.html#

我假設這是一些CORS問題,所以我已將此添加到頁面的html中

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; media-src *; img-src * filesystem: data:"> 

,這對我的config.xml中(我用的PhoneGap構建)

<plugin name="cordova-plugin-whitelist" source="npm" /> 

<allow-navigation href="*" /> 
<allow-navigation href="*://*/*" /> 
<allow-navigation href="file:///*/*" /> 
<allow-navigation href="http://*/*" /> 
<allow-navigation href="https://*/*" /> 
<allow-navigation href="data:*" /> 

<allow-intent href="*" /> 
<allow-intent href="http://*/*" /> 
<allow-intent href="https://*/*" /> 
<allow-intent href="tel:*" /> 
<allow-intent href="sms:*" /> 
<allow-intent href="mailto:*" /> 
<allow-intent href="geo:*" /> 

<access origin="*" /> 
<access origin="*://*/*" /> 
<access origin="file:///*" /> 
<access origin="cdvfile://*" /> 
<access origin="content:///*" /> 

正如你可以看到我已經想盡設置我能想到的,從網上淘。 我錯過了一些明顯的東西嗎?

在此先感謝。

+0

順便說一句,在雙方的iOS出現錯誤和Android –

回答

8

OK終於得到了一個解決方法,即將文件:/// URI轉換爲cdvfile:// URI,我的網頁只會抱怨它是混合內容警告,並且允許我訪問!

function getFileEntry(imgUri) { 
      window.resolveLocalFileSystemURL(imgUri, function success(fileEntry) { 
       console.log("got file: " + fileEntry.fullPath); 
       console.log('cdvfile URI: ' + fileEntry.toInternalURL()); 
       $('#imgPreview').attr("src", fileEntry.toInternalURL()); 
      }); 
     } 

仍然會是不錯的訪問文件有道:/// URI的,我可以看到這是行不通的情況下,但對於我這樣做有什麼解決。

+1

這需要此科爾多瓦插件https://github.com/apache/cordova-plugin-file –

6

這裏有一些需要注意的地方,以防未來遇到此問題的用戶遇到這種情況,請確保在測試此類功能時不會以'Live Reload'模式運行。 Live Reload會導致同樣的錯誤信息,這顯然會造成誤導。在構建完w/o live reload之後,使用文件:/ OR cdv:/ path對我來說一切正常。

+0

很高興在這裏看到這一點,只是細節是,它可能是對上述答案的評論......因爲它不是這樣的答案 –

1

我最近面臨同樣的問題。顯示cordova ios應用程序在內部運行在localhost:8080上,這是它不允許從「file://」協議加載文件的主要原因。

簡單修復 - 「var workingPath = window.Ionic.normalizeURL(givenPath)」;

請從離子閱讀文章有關 - https://ionicframework.com/docs/wkwebview/

+0

有趣的文章,不幸的是它只適用於離子3 –

+0

它幫助我,雖然我使用離子1 –

+0

最重要的是解釋了發生這種情況的原因。以及如何解決它一般。 –

相關問題