Android ImageView内容在显示后消失

时间:2014-05-10 21:56:45

标签: android imageview

我的应用程序执行此操作:拍摄照片,然后在ImageView中显示照片。奇怪的是,照片显示约一秒钟(在用相机拍摄之后),然后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();                  
                }
        }
    }
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您正在使用setBackgroundDrawable(Drawable drawable)来设置视图的背景。

如果您想更改ImageView的内容,您需要使用

setImageDrawable(Drawable drawable)

setImageBitmap(Bitmap bm)

答案 1 :(得分: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);
    }   
}

现在正在运作。

不确定如何解决这个问题。