更改为纵向/横向时 ImageView 消失
我有一个带有位图图片的图像视图。我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当方向改变时,活动被破坏并再次创建。如果您使用 xml 创建布局,则所有设置了 @id 的小部件都应该使用其内容自动重新创建。否则,您必须手动执行此操作 - 查看 Activity 的方法
onRestoreInstanceState() onSaveInstanceState()
只需覆盖它们并编写您自己的保存/恢复代码。希望一些未经测试的代码能给您一个想法:
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:
如果你给你的图像视图一个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.
根据 piotrpo 的答案,这里有一个更具体的版本:
其中
imageMap
是您的位图图像Building off piotrpo's answer, here is a more specific version:
where
imageMap
is your bitmap image