2014-05-10 47 views
0

我的應用程序執行此操作:拍攝照片,然後在ImageView中顯示照片。奇怪的是,照片顯示約一秒鐘(用相機拍攝後),然後ImageView再次變空。Android ImageView內容在顯示後消失

這是我的代碼:

publish.xml

<ImageView 
    android:id="@+id/itemImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 

PublishActivity.java

package ar.com.guiagratis; 

import java.io.File; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.BitmapDrawable; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class PublishActivity extends Activity { 
    final int TAKE_PICTURE_REQUEST_CODE = 115; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.publish);   
    } 

    public void btnNextClick(View v) { 
     // TODO: disable all buttons 
     //Intent intent=new Intent(getApplicationContext(), TakePhotoActivity.class); 
     // startActivityForResult(intent, TAKE_PICTURE_RESULT_CODE);  

     Toast.makeText(getApplicationContext(), "Sacate una foto viteh", Toast.LENGTH_SHORT).show(); 

     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
     File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png"); 
     Uri imageUri = Uri.fromFile(photoFile); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
     startActivityForResult(intent, TAKE_PICTURE_REQUEST_CODE); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) {   
     switch (requestCode) { 
      case TAKE_PICTURE_REQUEST_CODE: 
       if (resultCode == Activity.RESULT_OK) { 
        File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png"); 
        Uri imageUri = Uri.fromFile(photoFile); 

        // Image captured and saved to fileUri specified in the Intent 
        Toast.makeText(this, "Image saved to:\n" + 
          imageUri, Toast.LENGTH_LONG).show(); 

        Bitmap myBitmap = BitmapFactory.decodeFile(imageUri.getPath()); 
        BitmapDrawable ob = new BitmapDrawable(myBitmap); 
        ImageView myImage = (ImageView) findViewById(R.id.itemImage); 
        myImage.setBackgroundDrawable(ob); 

        Toast.makeText(getApplicationContext(), "Qué linda foto! ", Toast.LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(getApplicationContext(), "Hubo un problema al subir la imágen... ", Toast.LENGTH_SHORT).show();     
       } 
     } 
    } 
} 

任何幫助將不勝感激。

+1

'switch'中的'case'沒有'break' – deadfish

+0

你說得對。這不是一個錯誤,因爲我沒有其他CASE,但是我會添加這個中斷,因爲它可能會產生未來的錯誤。謝謝! – hhaamm

回答

0

好吧,我終於發現了問題。我沒有意識到我需要使用onSaveInstanceState和onRestoreInstanceState來存儲我不想從Activity中鬆開的值,比如啓動攝像頭並拍攝照片。

所以我添加以下代碼:

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
    // Save UI state changes to the savedInstanceState. 
    // This bundle will be passed to onCreate if the process is 
    // killed and restarted. 
    savedInstanceState.putBoolean("photoUploaded", photoUploaded); 
} 

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    // Restore UI state from the savedInstanceState. 
    // This bundle has also been passed to onCreate. 
    photoUploaded = savedInstanceState.getBoolean("photoUploaded"); 

    if (photoUploaded) { 
     File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png"); 
     Uri imageUri = Uri.fromFile(photoFile); 

     Bitmap myBitmap = BitmapFactory.decodeFile(imageUri.getPath()); 
     BitmapDrawable ob = new BitmapDrawable(myBitmap); 
     ImageView myImage = (ImageView) findViewById(R.id.uploadedImage); 
     myImage.setBackgroundDrawable(ob); 
    } 
} 

,現在正在工作。

不知道該如何解決這個問題。

2

您正在使用設置視圖背景的setBackgroundDrawable(Drawable drawable)

如果你想改變的ImageView的內容,您需要使用

setImageDrawable(Drawable drawable) 

setImageBitmap(Bitmap bm)