Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

android - How to load images from res/drawable-folder in ReactNative-App?

I'd like to display static images from res/drawable-folder in a ReactNative-App. However, there's nothing displayed.

I have the following folder structure in the android-subfolder:

macbook:CompanyApp hagen$ tree android/app/src/main/res/
android/app/src/main/res/
├── drawable
│?? ├── a1456.jpg
│?? └── a1457.jpg
├── mipmap-hdpi
│?? └── ic_launcher.png
├── mipmap-mdpi
│?? └── ic_launcher.png
├── mipmap-xhdpi
│?? └── ic_launcher.png
├── mipmap-xxhdpi
│?? └── ic_launcher.png
└── values
    ├── strings.xml
    └── styles.xml

6 directories, 8 files

As mentioned in the documentation, i try to load the image-files from drawable-folder with the following code:

<Image source={{uri: 'a1456'}} style={{width: 140, height: 140}} />

I also tried the filename together with the extension:

<Image source={{uri: 'a1456.jpg'}} style={{width: 140, height: 140}} />

and also:

<Image source={require('image!a1456')} style={{width: 140, height: 140, backgroundColor: 'yellow'}} />

And i tried the subfolder:

<Image source={{uri: 'drawable/a1456.jpg'}} style={{width: 140, height: 140}} />

No image will be displayed. Using a require-statement within image source for a local image in my current folder for instance is working fine.

But i am looking for a solution to consume the drawable-images like within a real native android application.

UPDATE

I just published an example project on github: https://github.com/itinance/testdrawable

It is a standard RN-App with "react-native init" and just added an image to display in drawable-folder (android only).

Can someone have a look at it and tell me what i am doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finally i got it: the <Image> Tag needs concrete "width" and "height" arguments as attributes, not only as style-parameter.

With that in mind, both declarations will work perfectly:

  <Image width={200} height={267} source={{uri: 'img_3665'}} style={{width: 200, height: 267}} />
  <Image width={200} height={267} source={require('image!img_3665')} style={{width: 200, height: 267}} />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...