4

在显示在 webview 中的本地 html 文件中,我希望能够从应用程序数据目录中调用图像。我在我的 html 中尝试了几个 src 选项,但似乎都没有。

我正在从应用程序数据目录成功加载 html 文件,如下所示:

var win = Ti.UI.currentWindow;

var rootDir = Ti.Filesystem.getExternalStorageDirectory();
var webUrl = rootDir + 'index.html';

var webview =  Ti.UI.createWebView({
width:'100%',
height:'100%',
url:webUrl
});

win.add(presWebView)

尽管页面在 web 视图中正确打开,但所有图像 url 都不起作用。

<image src="appdata:///image.jpg"/>
<image src="app:///image.jpg"/>
<image src="file:///image.jpg"/>
<image src="appdata://image.jpg"/>
<image src="app://image.jpg"/>
<image src="app://image.jpg"/>

这个问题也延伸到链接,无论我如何尝试引用它们,webview 都会告诉我页面不会退出。

4

3 回答 3

2

我已经解决了。

您必须获取 ApplicationDataDirectory o ExternalStorageDirectory,然后使用(文件对象的).nativePath 属性,您可以动态获取相对路径。

var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory(), 'test/1.html'); enter 

mywebview = Ti.UI.createWebView({ backgroundColor: 'white', url: file.nativePath });
于 2012-04-24T12:46:11.170 回答
0

我已经管理了一个部分解决方案,通过将 webview url 从基于其相对位置的地址更改为 externalstorage 目录的绝对路径,使图像和链接在我的本地 html 文件中正常工作:

var webUrl = "file:///mnt/sdcard/..."

var webview =  Ti.UI.createWebView({
width:'100%',
height:'100%',
url:webUrl
});

win.add(presWebView);

只要 url 是绝对路径,html 中的所有链接都可以是相对的,例如

显然,动态获取外部存储目录会更好,所以如果有人知道在不破坏 html 链接的情况下做到这一点的方法,我会很想听听。

于 2011-12-08T18:01:51.820 回答
0

在您的 Android 目录 /tools 中,有一个名为 DDMS 的小程序。如果你打开它,点击你的模拟器,然后在顶部菜单中,点击文件管理器,你可以看到所有的路径

"file://"

可以访问..

例如:

var imgPath = "file://mnt/sdcard/uk.co.yourappid/test.jpg";

我现在刚刚在 WebView 中工作:

Ti.App.addEventListener('snagr:plan:loadImage', function(e) {
            $('#plan-image').css({
                background: "url(" + e.path + ") top left no-repeat",
                width: e.width,
                height: e.height
            });
        });

希望这可以帮助!

于 2012-02-22T17:36:07.923 回答