You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何动态设置VideoView与ImageView的边距、宽度和高度?

Displaying Images in Xamarin.Android (Visual Studio) from Resources or Filesystem

Hey there! I totally get the frustration—most Android image display guides are written for native Android Studio (Java/Kotlin), so they don’t play nicely with Xamarin’s C# environment. Let’s fix that by walking through exactly how to show images from Android resources or the filesystem in your Main.axml layout.


1. Displaying an Image from Android Resources

This is the simplest case for static images bundled with your app.

Step 1: Add the Image to Your Project

  • Place your image file (e.g., my_photo.png) in the Resources/drawable folder of your Xamarin.Android project.
    • Important: Use lowercase filenames with no spaces or special characters (e.g., my_photo.png instead of My Photo!.png).

Step 2: Reference the Image in Main.axml

You can set the image directly in XML or dynamically via C#.

XML Approach

Update your Main.axml to include an ImageView with the android:src attribute pointing to your drawable:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Static ImageView from resources -->
    <ImageView
        android:id="@+id/resourceImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_photo" /> <!-- No file extension needed -->

</RelativeLayout>

C# Dynamic Approach

If you need to change the image at runtime, use this code in your MainActivity.cs:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.Main);

    // Get reference to the ImageView
    ImageView resourceImageView = FindViewById<ImageView>(Resource.Id.resourceImageView);
    // Set the image from resources
    resourceImageView.SetImageResource(Resource.Drawable.my_photo);
}

2. Displaying an Image from the Filesystem

For images stored on the device’s internal or external storage, we’ll use BitmapFactory to load the file.

Step 1: Add Permissions

First, ensure your app has permission to read external storage. Add these lines to AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- Add this if you need to write to storage too -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Note: For Android 6.0 (API 23+) and above, you’ll also need to request these permissions dynamically at runtime.

Step 2: Load the Image via C#

Use BitmapFactory.DecodeFile to load the image file and set it to your ImageView:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.Main);

    ImageView fileImageView = FindViewById<ImageView>(Resource.Id.fileImageView);
    // Example path: External storage root + your image file
    string imageFilePath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "my_external_photo.png");

    // Load the bitmap
    Bitmap imageBitmap = BitmapFactory.DecodeFile(imageFilePath);
    // Set it to the ImageView
    fileImageView.SetImageBitmap(imageBitmap);
}

Optional: Compress Large Images

To avoid OutOfMemory errors with big images, add compression options:

BitmapFactory.Options options = new BitmapFactory.Options();
options.InSampleSize = 2; // Reduces image size to 1/2 of original (adjust as needed)
Bitmap compressedBitmap = BitmapFactory.DecodeFile(imageFilePath, options);
fileImageView.SetImageBitmap(compressedBitmap);

Key Difference from Android Studio Code

The main reason your previous code didn’t work is that Xamarin uses C# syntax instead of Java. For example:

  • Java: imageView.setImageResource(R.drawable.my_photo);
  • Xamarin C#: imageView.SetImageResource(Resource.Drawable.my_photo);

Capitalization and namespace differences matter!

内容的提问来源于stack exchange,提问作者Luiey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:59:16