gpt4 book ai didi

Android-更改 fragment 时将ImageView中的图像保留在 fragment 上

转载 作者:行者123 更新时间:2023-11-30 02:03:46 25 4
gpt4 key购买 nike

我有下面的代码。我将图像从 fragment 中的图库设置为 ImageView。问题是当我更改 fragment 并使用 ImageView 再次返回 fragment 时,它具有默认图像,而不是我设置的图像。

public class Fragment_user extends Fragment {

private static final int SELECT_PICTURE = 1;
private String selectedImagePath=null;
private ImageView img;

@Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_user, container, false);
//if (savedInstanceState == null){
img = (ImageView)rootView.findViewById(R.id.userImage);
//}else{
//Bitmap bitmap = (Bitmap) savedInstanceState.getParcelable("bitmap");
//img.setImageBitmap(bitmap);
//}


img.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
return rootView;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == MainActivity.RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}

public String getPath(Uri uri)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
if (cursor == null) return null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String s=cursor.getString(column_index);
cursor.close();
return s;
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the image bitmap into outState
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
outState.putParcelable("bitmap", bitmap);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Read the bitmap from the savedInstanceState and set it to the ImageView
if (savedInstanceState != null){
Bitmap bitmap = (Bitmap) savedInstanceState.getParcelable("bitmap");
img.setImageBitmap(bitmap);
}
}

}

我使用 onSaveInstanceState 方法将所选图像保存到位图,使用 onActivityCreated 方法在我返回 Fragment 时将图像设置回 ImageView。但这不起作用。有什么帮助吗?提前致谢

最佳答案

发生这种情况是因为您的 fragment 根据 Fragments Lifecycle 失去了 View .要解决您的问题,请执行下一步操作:

  1. 创建全局变量private Uri mSelectedImageUri;

  2. 使用此变量代替您的本地 Uri selectedImageUri:

    if (requestCode == SELECT_PICTURE) {
    mSelectedImageUri = data.getData();
    selectedImagePath = getPath(mSelectedImageUri);
    System.out.println("Image Path : " + selectedImagePath);
    img.setImageURI(mSelectedImageUri);
    }
  3. 在此行下方添加检查 img = (ImageView)rootView.findViewById(R.id.userImage);:

    if (mSelectedImageUri != null) {
    img.setImageURI(mSelectedImageUri);
    }

关于Android-更改 fragment 时将ImageView中的图像保留在 fragment 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31082939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com