Phonegap 图像未显示

IT技术 image reactjs phonegap-build
2021-04-27 05:12:54

我无法让我的图像在我的 phonegap 版本中工作。

我读过绝对路径可能不起作用,所以我尝试了绝对路径和相对路径,但仍然没有运气。

我包括这样的图像:

<Col key={1} xs={3}>
  <Image src='/tire_selected.png' responsive />
</Col>

或亲戚

<Col key={1} xs={3}>
  <Image src='tire_selected.png' responsive />
</Col>

等于

<img class="img-responsive" src="tire_deselected.png" data-reactid=".0.0.1.0.0.0.0.1.1.0.0.$4.0">

Col & Image 是使用 bootstrap-react 的 bootstrap helper 组件。这一切在 web 视图中都可以正常工作,但在使用 phonegap 构建时就不行了。尽管如此,源代码已经编译并且在两种情况下都没有错误。

以下是我的 config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.app.exampleapp" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
    <name>App</name>
    <description>
    App
    </description>
    <author email="support@example.com" href="http://www.example.com">
        Author
    </author>
    <content src="index.html" />
    <preference name="permissions" value="none" />
    <preference name="orientation" value="default" />
    <preference name="target-device" value="universal" />
    <preference name="fullscreen" value="true" />
    <preference name="webviewbounce" value="true" />
    <preference name="prerendered-icon" value="true" />
    <preference name="stay-in-webview" value="false" />
    <preference name="ios-statusbarstyle" value="black-opaque" />
    <preference name="detect-data-types" value="true" />
    <preference name="exit-on-suspend" value="false" />
    <preference name="show-splash-screen-spinner" value="true" />
    <preference name="auto-hide-splash-screen" value="true" />
    <preference name="disable-cursor" value="false" />
    <preference name="android-minSdkVersion" value="14" />
    <preference name="android-installLocation" value="auto" />
    <gap:plugin name="org.apache.cordova.geolocation" />
    <icon src="icon.png" />
    <access origin="*" />
    <plugin name="cordova-plugin-whitelist" version="1" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

Git存储库:

    app.js
    vendor.js
    config.xml
    favicon.ico
    humans.txt
    index.html
    robots.txt
    tire_deselected.png
    tire_selected.png

Icon.png 工作正常。我不知道是什么导致其他图像不起作用。任何帮助,将不胜感激!

编辑

我试过设置内容安全策略,如果这是我无法设置 img-src 并通过 javascript 显示图像的问题。

<meta http-equiv="Content-Security-Policy" content="
  default-src http://10.3.10.104/ 'self' * 'unsafe-inline';
  style-src http://10.3.10.104/ 'self' * 'unsafe-inline';
  img-src http://10.3.10.104/ 'self' * 'unsafe-inline';
  script-src http://10.3.10.104/ 'self' * 'unsafe-inline';">

但仍然没有运气

file:///tire_deselected.png net::ERR_FILE_NOT_FOUND

那里有文件,因为在将 img 元素插入 index.html 时,它会显示出来。

我什至尝试通过运行开发人员工具的源文件夹中显示的路径访问它。

file:///data/user/0/com.oas.exampleapp/files/downloads/app_dir/tire_deselected.png 

也不起作用,我开始认为 phonegap 已损坏,至少与 react 结合使用时效果很差。

3个回答

编译后build.phonegap.com将您的源文件放入“www”目录。

您可以使用以下路径“/android_asset/www/”访问您的本地图像文件

<image src='/android_asset/www/tire_selected.png' responsive />

如果您的图像放在根目录内的子目录中,则可以使用以下内容:

<image src='/android_asset/www/sub-direcctory/tire_selected.png' responsive />

注意:如果有任何包含本地图像文件的“子目录”替换为您自己的。

我在 index.html 中添加了一个 img 标签,并将 src 属性设置为“images/logo.png”,它加载没有问题。

...
  <body>
    <div id="root"></div>
      <img id="footer-logo" src="images/logo.png" style="background-color: black; width: 200px;" />
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script src="js/vendor.bundle.js"></script>
    <script src="js/app.bundle.js?v=2"></script>
  </body>
</html>

我有一个带有 img 标签和相同 src 值“images/logo.png”的react组件

...
<div style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin: 0px; padding-top: 0px; letter-spacing: 0px; font-size: 24px; font-weight: 400; color: rgb(48, 48, 48); height: 64px; line-height: 64px; flex: 1 1 0px; text-align: center;">
  <img id="header-logo" src="images/logo.png" style="width: 105px; margin-top: 16px;">
</div>
...

react组件中的img没有加载;404.然而这等于是真的

document.getElementById('footer-logo').src === document.getElementById('header-logo').src

为什么其中一个图像加载而另一个没有?它与动态加载到 DOM 中的 react 组件或 react 的虚拟 DOM 有关系吗?

src 属性等同于file:///images/logo.png. 如果我像这样在 #header-logo 上设置 src 属性,它会加载:

document.getElementById('header-logo').src = cordova.file.applicationDirectory + "www/images/logo.png"

希望这为这种非常奇怪的行为提供更多信息。

希望这可以帮助。所以我也遇到了这个问题。

我所做的是创建另一个文件夹/images/(重复)并仍然使用我通过我的/static/components/images文件夹通过react导入的图像您可以通过为暂存或实时添加条件来更进一步。

所以答案在这里。:

import Logo from '../images/logo.png';
<img src={`./${Logo}`} alt="Logo" />

完整示例:

import React, { Component } from 'react';
import Logo from '../images/logo.png';

class Header extends Component {
    render() {
        return(
            <div className="logo mt-3">
                <img src={`./${Logo}`} alt="Logo" />
            </div>
        );
    }
}

export default Header;

从这篇文章中得到了想法。:PhoneGap Build 应用程序中未显示图像