2
votes

I have a Flutter project that works well on both the iOS and Android platform.

In the Flutter project, there is an image located in android/app/src/main/res/mipmap-xxhdpi/temp.jpg,

and I want to load that image and use it in the Flutter widget.

I have tried to find answer in the document Adding assets and images and Flutter for Android developers but no luck.

How to do that? Thanks.

2
Flutter for Android mentioned that you can access images directly in an image widget. For example, Image.asset("images/my_image.png");. Have you tried this?jabamataro
@Uni I know the way you mentioned, and it do works, but I was wondering how to access images that are located in path that I mentioned in the question. Or putting images in that location is a bad idea?Shan
@jabamataro Yes, it works when I access the images located under the images folder, but not works for the path that I mentioned in the question.Shan
@Shan have you tried changing the path to Image.asset("android/app/src/main/res/mipmap-xxhdpi/temp.jpg");? Note that you need to define it first in pubspec.yamljabamataro
@jabamataro It works!! You save my day, thanks! I am new to Flutter so did not realized it can work like that. I will update the answer based on your comments later. Really appreciate your help!Shan

2 Answers

3
votes

Just like @jabamataro mentioned in the comments, define the path - android/app/src/main/res/mipmap-xxhdpi/ in the assets: section of the pubspec.yaml file like below:

assets:
    - android/app/src/main/res/mipmap-xxhdpi/

then the image could be used like:

AssetImage('android/app/src/main/res/mipmap-xxhdpi/temp.jpg');  

Hope this answer can help those new to Flutter development.

1
votes

Load images folder in pubspec.yaml like

flutter:
  assets:
    - assets/images/

and then use it in image widget like

child: Image.asset('assets/images/splash_logo.png',
        height: 150,
        width: 150,
      fit: BoxFit.cover),