-1

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.

S.Krylov
  • 11
  • 1

2 Answers2

1

View is recreated when : - the screen is turnd off and back on, - screen rotate

so when the view is created as default

 <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView1" />

you dont have any image there... thats why its dissappering.

you need to save your current state so that when view is recreated everything will be back on

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Read values from the "savedInstanceState"-object and put them in your textview
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // Save the values you need from your textview into "outState"-object
    super.onSaveInstanceState(outState);
}
laymelek
  • 418
  • 2
  • 15
  • Could you explain about "you need to save your current state so that when view is recreated everything will be back on" .Sorry ,I do not know much English. – S.Krylov May 20 '14 at 10:25
0

as laymelek said, the reason why your image disappears when screen rotates is that the activity is destroyed and recreated. During such process, things like the image selected or other application specific status are not retained.

Usually custom application status info are retained using activity's InstanceState, as explained by laymelek. But this is not the suggested behavior in case of Bitmaps or large objects.

In such case you should retain the image in memory and you can do the trick with a custom Fragment, as suggested by official documentation

You can have a look at my answer to this other question

Community
  • 1
  • 1
ilmatte
  • 1,732
  • 16
  • 12