简体   繁体   English

在appcelerator / titanium mobile中,当在Webview中使用本地html时,如何从应用程序数据目录加载图像并引用该图像

[英]In appcelerator/titanium mobile, when using local html in a webview, how do I load an image from, and reference, the application data directory

In a local html file, that is displayed in a webview, I want to be able to call an image from the application data directory. 在Web视图中显示的本地html文件中,我希望能够从应用程序数据目录中调用图像。 I have tried several src options in my html, but none seem to work. 我在html中尝试了几个src选项,但似乎都没有用。

I am loading the html file from the application data directory successfully like this: 我正在从应用程序数据目录成功加载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)

Although the page opens in the webview correctly, all the image urls are not functional. 尽管页面可以在Webview中正确打开,但是所有图像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"/>

this problem also extends to links, no matter how I try to reference them the webview tells me the page does not exit. 此问题也扩展到链接,无论我如何尝试引用它们,webview都会告诉我该页面不会退出。

I've solved it. 我已经解决了

You must obtain ApplicationDataDirectory o ExternalStorageDirectory, then with .nativePath property (of the file object) you can obtain your relative path dynamically. 您必须获取ApplicationDataDirectory或ExternalStorageDirectory,然后使用(文件对象的).nativePath属性可以动态获取相对路径。

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

mywebview = Ti.UI.createWebView({ backgroundColor: 'white', url: file.nativePath });

I have managed a partical solution which gets both the images and the links working correctly in my local html file by changing the webview url from an address based on its relative position to the externalstorage directory, to an absolute path: 我已经管理了一个局部解决方案,通过将Webview URL从基于其相对位置的地址更改为externalstorage目录,到绝对路径,使图像和链接在我的本地html文件中都能正常工作:

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

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

win.add(presWebView);

As long as the url is an absolute path all the links in the html can be relative eg 只要url是绝对路径,html中的所有链接都可以是相对的,例如

Obviously it would be preferable to get the external storage directory dynamically, so if anyone knows a way to do this without breaking the links in the html I would be keen to hear. 显然,最好动态获取外部存储目录,因此,如果有人知道一种方法而又不破坏html中的链接,我将很想听听。

In your Android directory /tools, there is a little program called DDMS. 在您的Android目录/ tools中,有一个名为DDMS的小程序。 If you open that up, click on your emulator then in the top menu, click File Manager you can see all the paths that 如果您打开它,请单击模拟器,然后在顶部菜单中单击“文件管理器”,您可以看到所有

"file://"

Can access.. 可以访问..

So for example: 因此,例如:

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

I have just got this working in a WebView now: 我现在已经在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
            });
        });

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用android webview在appcelerator的钛移动应用程序上运行地理定位 - How to get geolocation working on appcelerator's titanium mobile application with android webview 如何使用钛 appcelerator 在我的 android 应用程序中播放.asx 文件? - How do i play .asx files in my android application using titanium appcelerator? 从webview中的本地专用目录加载图像 - Load Image from local private directory in a webview ¿如何通过本地html文件从此处加载webView? - ¿How can I load a webView from here with a local html file? 在移动设备上,我可以使用WebView从html提取数据吗? - On a mobile device, can I extract data from html using a WebView? 在Android(Titanium Appcelerator)中使用Sanselan从图像获取元数据 - Getting metadata from image using Sanselan in android(Titanium Appcelerator) 如何将二元数据字符串转换为钛制Appcelerator中的图像? - how to convert binary data string into image in titanium appcelerator? 如何使用Appcelerator Titanium在Web应用程序中显示数学符号? - How to show math symbols in a web application using Appcelerator titanium? 使用 Appcelerator Titanium 开发移动应用程序 - Developing mobile apps using Appcelerator Titanium Appcelerator Titanium Mobile和Airpush - Appcelerator Titanium Mobile and Airpush
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM