onSaveInstanceState 保存 ImageButton 的状态

发布于 2024-12-12 20:03:47 字数 1417 浏览 4 评论 0原文

我在注册过程中使用 ImageButton 选择用户的个人资料照片。处理纵向和横向模式之间的旋转。我想保存背景的状态。

对于 EditText 我正在这样做

onSaveInstanceState

EditText FirstName = (EditText) findViewById(R.id.FirstName);
String firstName = FirstName.getText().toString();
savedInstanceState.putString("FirstName", firstName);

onRestoreInstanceState

    String firstName = savedInstanceState.getString("FirstName");
    if (firstName != null) {
        EditText FirstName = (EditText) findViewById(R.id.FirstName);
        FirstName.setText(firstName);
    }

我怎样才能对 ImageButton 做同样的事情?

我像这样设置 ImageButton

            Uri selectedImageUri = data.getData();

            // MEDIA GALLERY
            selectedImagePath = getPhotoPath(selectedImageUri);

            // OI FILE Manager
            filemanagerstring = selectedImageUri.getPath();

            // NOW WE HAVE OUR WANTED STRING
            if (selectedImagePath != null) {
                SelectedPhoto = BitmapFactory.decodeFile(selectedImagePath);
                DisplayPhoto.setImageBitmap(SelectedPhoto);
                String bans = DisplayPhoto.getBackground().toString();
                Log.i("Bakasura", bans);
            } else{
                SelectedPhoto = BitmapFactory.decodeFile(filemanagerstring);
                DisplayPhoto.setImageBitmap(SelectedPhoto);
            }

I am using an ImageButton to select Profile Photo of the user in my sign up process. To handle the rotation between portrait and landscape mode. i want to save the state of the background.

For the EditText i am doing this

onSaveInstanceState

EditText FirstName = (EditText) findViewById(R.id.FirstName);
String firstName = FirstName.getText().toString();
savedInstanceState.putString("FirstName", firstName);

onRestoreInstanceState

    String firstName = savedInstanceState.getString("FirstName");
    if (firstName != null) {
        EditText FirstName = (EditText) findViewById(R.id.FirstName);
        FirstName.setText(firstName);
    }

How can i do the same for the ImageButton ?

i am setting the ImageButton like this

            Uri selectedImageUri = data.getData();

            // MEDIA GALLERY
            selectedImagePath = getPhotoPath(selectedImageUri);

            // OI FILE Manager
            filemanagerstring = selectedImageUri.getPath();

            // NOW WE HAVE OUR WANTED STRING
            if (selectedImagePath != null) {
                SelectedPhoto = BitmapFactory.decodeFile(selectedImagePath);
                DisplayPhoto.setImageBitmap(SelectedPhoto);
                String bans = DisplayPhoto.getBackground().toString();
                Log.i("Bakasura", bans);
            } else{
                SelectedPhoto = BitmapFactory.decodeFile(filemanagerstring);
                DisplayPhoto.setImageBitmap(SelectedPhoto);
            }

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

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

发布评论

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

评论(3

酒废 2024-12-19 20:03:47

由于您要专门保存状态来处理旋转,因此最好的选择是使用 onRetainNonConfigurationInstance;正是为了这样的目的而包括在内。在 Activity 中重写此方法以返回图像本身(或包含图像的对象):

public Object onRetainNonConfigurationInstance()
{
   return bitmapImage;
}

然后准备在 onCreate 中再次接收它:

Object last = getLastNonConfigurationInstance();
if( last != null )
{
   DisplayPhoto.setImageBitmap( (Bitmap) last );
}

请注意,在 Honeycomb 及更高版本中,您应该使用 setRetainInstance() 在片段上,而不是使用此方法;但在不早于 Honeycomb 的兼容性可接受之前,这是最好的解决方案。

Since you're saving the state specifically to deal with rotation, your best option here is to use onRetainNonConfigurationInstance; included for just such a purpose. Override this method in the Activity to return the image itself (or an object which contains it):

public Object onRetainNonConfigurationInstance()
{
   return bitmapImage;
}

Then be prepared to receive it again in onCreate:

Object last = getLastNonConfigurationInstance();
if( last != null )
{
   DisplayPhoto.setImageBitmap( (Bitmap) last );
}

Note that in Honeycomb and later, you should use the setRetainInstance() on a fragment, rather than use this method; but until compatibility no earlier than Honeycomb is acceptable, this is the best solution.

梦毁影碎の 2024-12-19 20:03:47

您可以保存图像的uri
并使用

imagebutton.setImageURI(uri);

you can save uri of the image.
and use

imagebutton.setImageURI(uri);
救赎№ 2024-12-19 20:03:47

您无法使用 = 检查字符串
IE。

Uri selectedImageUri = data.getData();
String selectedImagePath ="";
selectedImagePath = getPhotoPath(selectedImageUri);

          if (!selectedImagePath.equalsIgnoreCase("")) {
                SelectedPhoto = BitmapFactory.decodeFile(selectedImagePath);
                DisplayPhoto.setImageBitmap(SelectedPhoto);
                String bans = DisplayPhoto.getBackground().toString();
                Log.i("Bakasura", bans);
            } else{
                SelectedPhoto = BitmapFactory.decodeFile(filemanagerstring);
                DisplayPhoto.setImageBitmap(SelectedPhoto);
            }

you cant check String with =
ie.

Uri selectedImageUri = data.getData();
String selectedImagePath ="";
selectedImagePath = getPhotoPath(selectedImageUri);

          if (!selectedImagePath.equalsIgnoreCase("")) {
                SelectedPhoto = BitmapFactory.decodeFile(selectedImagePath);
                DisplayPhoto.setImageBitmap(SelectedPhoto);
                String bans = DisplayPhoto.getBackground().toString();
                Log.i("Bakasura", bans);
            } else{
                SelectedPhoto = BitmapFactory.decodeFile(filemanagerstring);
                DisplayPhoto.setImageBitmap(SelectedPhoto);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文