ImageView的 - 像失去了对方向变化 [英] ImageView - image lost on change in orientation

查看:128
本文介绍了ImageView的 - 像失去了对方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的地方拍张照片,并将其设置在图像按钮图像的应用程序。在方向改变时,图像消失。请帮助。

I have an application where I take a picture and set it as the image on image button. On change in orientation, the image vanishes. Please help.

我点击一张图片并将其设置为上的ImageItem

I click a picture and set it as the image on the ImageItem

public void onImageButtonClicked(View view)
{
    Intent camIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camIntent, TAKE_PHOTO);
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PHOTO && resultCode == Activity.RESULT_OK)
        try {
            InputStream stream = getContentResolver().openInputStream(data.getData());
            Bitmap bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            ImageButton imageButton = (ImageButton) findViewById(R.id.itemImage);

            int mDstWidth = getResources().getDimensionPixelSize(R.dimen.destination_width);
            int mDstHeight = getResources().getDimensionPixelSize(R.dimen.destination_height);

            scaledBmp = Bitmap.createScaledBitmap(bitmap, mDstWidth, mDstHeight, true);
            imageButton.setImageBitmap(scaledBmp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    super.onActivityResult(requestCode, resultCode, data);
}

当的取向改变时,图像消失。

When the orientation is changed, image disappears.

I saved the image in 

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
    super.onSaveInstanceState(savedInstanceState);
    if (scaledBmp != null) {
        savedInstanceState.putByteArray("image", getBitmapAsByteArray(scaledBmp));
    }
}


and retrieved in 

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    byte[] savedImage;
    savedImage = savedInstanceState.getByteArray("image");
    if (savedImage != null) {
        scaledBmp = BitmapFactory.decodeByteArray(savedImage, 0, savedImage.length -1);
    }
}

我应该现在手动设置图像上的按钮?是否有什么我失踪?

Should I set the image on the button manually now? Is there something I am missing?

推荐答案

我失踪onResume()

I was missing onResume()

protected void onResume()
{
    super.onResume();
    if (scaledBmp != null)
    {
        ImageButton ib = (ImageButton) findViewById(R.id.itemImage);
        if (ib != null)
            ib.setImageBitmap(scaledBmp);
    }
}

随着这一变化,事情开始工作。我得到的景观形象,以及肖像模式。

With this change, things started working. I am getting the image in landscape as well as portrait mode.

Asthme的评论帮助我,谢谢。

Asthme's comment helped me, thanks.

这篇关于ImageView的 - 像失去了对方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆