我最近开始在已有的Android应用程序中集成react native。我尝试了多种图像加载方式,如页面https://facebook.github.io/react-native/docs/images.html
所述方法1(混合应用):对于Android使用<Image source={{uri: 'asset:/app_icon.png'}} style={{width: 40, height: 40}} />
此方法不会在应用程序上加载任何内容,只会加载40X40空白区域。
方法2:<Image source={require('./my-icon.png')} />
我使用了图像app/res/drawable-hdpi/image.png
的绝对路径,但它也没有用。
得到错误
Unable to resolve module `../app/res/drawable-hdpi/image.png` from
`/Users/MyName/StudioProjects/reatProject/android/react-native/index.js`: could not resolve
`/Users/MyName/StudioProjects/reatProject/android/app/res/drawable-hdpi/image.png' as a file nor as a folder
我总是在样式图中提供高度和宽度属性。
我的项目结构是
android/
app/
res/
src/
tests/
react-native/
index.js
package.json
package.lock.json
tests/
build.gradle
项目build.gradle
文件包含maven {@ 1}}
我发布了这个问题作为反应原生github问题的问题,但没有解决方案,所以问这里是否有人经历过同样的问题。我对新生的反应很新,所以也许我做错了。
答案 0 :(得分:0)
我最终犯了一个错误。图片来源必须采用{{uri:"name"}}
格式,并且我一直在撰写{{uri:"name.png"
}}。它不接受此方案中的png,但在asset://
和require
方案中,它确实如此。它只是有点混乱。
对于任何面临同样问题的人来说,相同的目录结构都可以使用,只需这样使用图像,并避免使用扩展名。
<Image source={{uri:"imageName"}} style={{height:100, width:100}}/>