2014-05-20 7 views
-1

나는 아주 간단한 예제를 이해합니다 - 안드로이드에있는 SD 카드의 imageview에 단일 이미지를 표시하십시오. 나는 USB를 통해 실제 장치에서 C# 및 디버그 응용 프로그램과 함께 Xamarin을 사용하고 있습니다.왜 화면을 돌릴 때 이미지가 사라 집니까?

구성 요소 "ImageView"는 이미지를 올바르게 표시하지만 화면을 회전하면 이미지가 표시되지 않습니다.

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> 

감사. 화면이 꺼져 turnd하고 다시, - - 화면이보기가 기본

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

당신이 거기에 어떤 이미지를 가지고 있겠지로 만들어집니다 그래서 때

회전 :

답변

1

보기는 때 다시 만들어집니다. .. 그게 왜 그 dissappering.

당신이보기 다시 때 laymelek 말했듯이 모든

@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); 
} 
+0

에 대한 내 대답에 모습을 가질 수 없습니다 당신은 메모리에 이미지를 유지해야하며, 사용자 정의 조각과 트릭을 할 수 있습니다 "당신이 현재의 상태를 저장해야 뷰가 다시 생성 될 때 모든 것이 다시 돌아올 것"에 대해 설명하십시오. 죄송합니다. 영어는 많이 모릅니다. –

+0

나는 내 대답을 편집했다. – laymelek

0

에 돌아올 수 있도록 현재 상태를 저장해야 화면이 회전 할 때 이미지가 사라지는 이유는 활동을 파괴하고 다시된다는 점이다 . 이러한 과정에서 선택한 이미지 또는 기타 응용 프로그램 특정 상태와 같은 항목은 유지되지 않습니다.

일반적으로 사용자 정의 응용 프로그램 상태 정보는 laymelek에서 설명한대로 활동의 InstanceState를 사용하여 유지됩니다. 그러나 비트 맵 또는 큰 개체의 경우 제안 된 동작이 아닙니다. official documentation

에 의해 제안 등의 경우

당신에게 당신이 다른 question

관련 문제