I understand a very simple example - display single image in imageview from sd card in android. I'am using Xamarin with C# and debug application on a real device via USB.
Component "ImageView" displays the image correctly, but when rotates the screen, image will not be displayed.
Activity1.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.IO;
using Android.Graphics;
namespace DisplayImageFromSDCard
{
[Activity(Label = "DisplayImageFromSDCard", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
ImageView imageView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
imageView = FindViewById<ImageView>(Resource.Id.imageView1);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += button_Click;
}
void button_Click(object sender, EventArgs e)
{
var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path;
var imageFilePath = System.IO.Path.Combine(sdCardPath, "SampleImageFile.jpg");
if (System.IO.File.Exists(imageFilePath))
{
var imageFile = new Java.IO.File(imageFilePath);
Bitmap bitmap = BitmapFactory.DecodeFile(imageFile.AbsolutePath);
imageView.SetImageBitmap(bitmap);
}
else
{
//Display No Image Found
}
}
}
}
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/MyButtonText" />
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView1" />
</LinearLayout>
Grateful for any help.