更改为纵向/横向时 ImageView 消失

发布于 2024-11-24 12:17:33 字数 855 浏览 0 评论 0原文

我有一个带有位图图片的图像视图。我在 onActivityResault 中设置了这张图片。问题是当我更改为纵向/横向时图像消失。

protected void onActivityResult(int requestCode,int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case 1:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            InputStream imageStream;
            try {
                imageStream = getContentResolver().openInputStream(selectedImage);
                Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                image.setImageBitmap(yourSelectedImage);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

I have an imageview with a bitmap picture on it. i set this picture in the onActivityResault. the problem is when i change to portrait/landscape the image disapears.

protected void onActivityResult(int requestCode,int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case 1:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            InputStream imageStream;
            try {
                imageStream = getContentResolver().openInputStream(selectedImage);
                Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                image.setImageBitmap(yourSelectedImage);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁微风不噪 2024-12-01 12:17:33

当方向改变时,活动被破坏并再次创建。如果您使用 xml 创建布局,则所有设置了 @id 的小部件都应该使用其内容自动重新创建。否则,您必须手动执行此操作 - 查看 Activity 的方法 onRestoreInstanceState() onSaveInstanceState() 只需覆盖它们并编写您自己的保存/恢复代码。
希望一些未经测试的代码能给您一个想法:

@Override
public void onSaveInstanceState(Bundle b){
    b.putParcelable("image", image.getBitmap());
}

@Override
public void onRestoreInstanceState(Bundle b){
    //you need to handle NullPionterException here.
    image.setBitmap((Bitmap) b.getParcelable("image"));
}

When orientation changes activity is destroyed and created again. If you're creating your layout using xml all widgets having @id set, should be automatically recreated with it's content. Otherwise you have to do that manually - take a look at Activity's methods onRestoreInstanceState() onSaveInstanceState() just override them and write your own save/restore code.
Some of untested code, hope, that it will give you an idea:

@Override
public void onSaveInstanceState(Bundle b){
    b.putParcelable("image", image.getBitmap());
}

@Override
public void onRestoreInstanceState(Bundle b){
    //you need to handle NullPionterException here.
    image.setBitmap((Bitmap) b.getParcelable("image"));
}
时光礼记 2024-12-01 12:17:33

如果你给你的图像视图一个ID,当你改变方向时它不会消失。我认为这是保持你的观点的最简单的方法。

If you give an ID to your imageview it won't be disappear, when you change orientation. I think it's the easiest way, to keep, your view.

倾`听者〃 2024-12-01 12:17:33

根据 piotrpo 的答案,这里有一个更具体的版本:

@Override
public void onSaveInstanceState(Bundle b){
    b.putParcelable("image", imageMap);
}

@Override
public void onRestoreInstanceState(Bundle b){
    imageMap = b.getParcelable("image");
    imageView.setImageBitmap(imageMap);
}

其中 imageMap 是您的位图图像

Building off piotrpo's answer, here is a more specific version:

@Override
public void onSaveInstanceState(Bundle b){
    b.putParcelable("image", imageMap);
}

@Override
public void onRestoreInstanceState(Bundle b){
    imageMap = b.getParcelable("image");
    imageView.setImageBitmap(imageMap);
}

where imageMap is your bitmap image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文