0

I'm currently working on my first .NET MAUI app and I'm encountering an issue with accessing uploaded images that reside in a folder in the API and rendering them from within the app.

In general, when working on Visual Studio's Windows Machine emulator, I can use https://localhost:7267 for everything, but for the Android emulator, I have to switch to http://10.0.2.2:5157. Note the switch from https to http. The app works both ways, until:

I have a user avatar feature where a user can upload an image on registration. When I'm running the app on the Windows machine using https://localhost:7267, I can access the image within my app and render it on the dashboard page. It's stored in the API, which is run at localhost. The app attempts to access the image at https://localhost:7267/Images\b30965a2-9aa8-4e0e-bbbd-a0b93fa6490a_Joe.jpeg, for example.

However, when I switch to the Android version using http://10.0.2.2:5157, the app works in all other senses, but the image is not rendering in the app. It's trying to access an image at http://10.0.2.2:5157/Images\\b30965a2-9aa8-4e0e-bbbd-a0b93fa6490a_Joe.jpeg, for example, but the image is not found.

It seems like a problem many must have come up against. Any ideas?

For more context:

I can access the image via the first URL in a web browser (https://localhost:7267/Images\b30965a2-9aa8-4e0e-bbbd-a0b93fa6490a_Joe.jpeg). I can't do this with the second using the 10.0.2.2 IP address (http://10.0.2.2:5157/Images\\b30965a2-9aa8-4e0e-bbbd-a0b93fa6490a_Joe.jpeg), which makes sense. I CAN access it via a browser if I change the Android IP back to localhost (http://localhost:5157/Images//b30965a2-9aa8-4e0e-bbbd-a0b93fa6490a_Joe.jpeg). However, my Android app still won't see the image when trying with either of these URLs.

1 Answer 1

3

Because you don't understand what an emulator is and what localhost is. Localhost means the current machine. The Android emulator is a computer emulator. It thinks it's a different machine. So going to localhost on the emulator only works if the software you want to connect to is running on the emulator- not if it's running on the host PC. The 10.0.2.2 IP address is the address of the host PC on the special VLAN the emulator sets up. That's the correct address to get to any content hosted on the host PC.

Also note your URL is invalid. http://10.0.2.2:5157/Images\\b30965a2-9aa8-4e0e-bbbd-a0b93fa6490a_Joe.jpeg is not the URL. http://10.0.2.2:5157/Images/b30965a2-9aa8-4e0e-bbbd-a0b93fa6490a_Joe.jpeg is. Even if Windows uses \ for the separator, web requests do not. Using \ is invalid and will not work for most servers.

Sign up to request clarification or add additional context in comments.

1 Comment

Calls to the API from the Android emulator are already working fine. It's just the image component that is the issue. And the URLs are generated that way with the backslashes by the app but are resolved by the API and web browser fine. I assumed the Android emulator would work in the same way, but at least that's given me something to look into.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.