无法在片段中的 onActivityResult 更新视图
Not able to update views at onActivityResult in fragment
我有一个片段,我在 onActivityResult
从我的自定义画廊获取图像数据然后我需要更新我的 TextView
和 ImageView
在 onActivityResult
调用.我从 Whosebug 和 GitHub 尝试了几种方法,但 none 对我有用。我在日志中检查了我使用这样的路径成功获取图像 /storage/emulated/0/DCIM/Camera/IMG_20190430_221429.jpg
我还检查了它是否有效。
上传图片片段
public class UploadImage_Fragment extends Fragment implements View.OnClickListener {
public static final String TAG="###Upload Image###";
TextView uploadText;
ImageView uploadImage;
Button uploadButton;
App_Functions functions;
String cmpImage,Base64Image;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.upload_profile_cover_registration,container,false);
uploadText = v.findViewById(R.id.textViewImageUpload);
uploadImage = v.findViewById(R.id.uploadImage);
uploadButton = v.findViewById(R.id.uploadButton);
uploadButton.setEnabled(false);
Glide.with(getActivity()).load(R.drawable.cam_icon).into(uploadImage);
uploadImage.setOnClickListener(this);
uploadButton.setOnClickListener(this);
setHasOptionsMenu(true);
return v;
}
@Override
public void onClick(View view) {
int clickID=view.getId();
switch (clickID){
case R.id.uploadImage:
Media_Gallery gallery = new Media_Gallery();
gallery.setTargetFragment(this,101);
FragmentTransaction fragmentTrasaction=getFragmentManager().beginTransaction();
fragmentTrasaction.addToBackStack("Chat");
fragmentTrasaction.replace(R.id.FragmentLoginRegistration,gallery,"Chat");
fragmentTrasaction.commit();
break;
case R.id.uploadButton:
if (uploadButton.isEnabled()){
Log.d(TAG,"Button Is Enable Upload Image Here");
uploadButton.setEnabled(false);
}else {
Toast.makeText(getActivity(), "Select Image To Upload", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
functions=new App_Functions(getActivity());
if (requestCode==301 && resultCode== Activity.RESULT_OK){
Uri uri = data.getData();
cmpImage = functions.compressImage(uri,true);
Glide.with(getActivity()).load(cmpImage).apply(RequestOptions.circleCropTransform()).into(uploadImage);
uploadButton.setEnabled(true);
Log.d(TAG,"Image "+uri+" Compress Image "+cmpImage);
Bitmap bitmap = BitmapFactory.decodeFile(cmpImage);
Base64Image=functions.Convert_To_Base64(bitmap);
}else if (requestCode == 101 && resultCode == Activity.RESULT_OK){
ArrayList<String> imagePath = data.getStringArrayListExtra("Profile");
if (imagePath!=null){
String image = imagePath.get(0);
Log.d(TAG,"Image Path "+image);
uploadText.setText("Hey I am New Test"); // Not working
uploadImage.setImageDrawable(null);
uploadImage.invalidate();
File file = new File(image);
Uri newPath = Uri.fromFile(file);
Log.d(TAG,"Check File "+file);
Log.d(TAG,"Check Uri "+newPath);
Log.d(TAG,"Check ImageView "+uploadImage);
Glide.with(getActivity()).load(image).into(uploadImage); // Not working
}
}
}
@Override
public void onResume() {
super.onResume();
uploadImage.setImageDrawable(null);
uploadImage.invalidate();
}
}
我尝试了一些方法,例如使无效、设置图像 view.setBitmapImage(null) 但 none 对我也有效 我也尝试更新 textview 但它对我也不起作用。请帮助
您不应该直接从 onActivityResult
方法更新视图,因为它可以在您的 activity 即将被销毁和重新创建之前调用。
Tal Kanel对此提供了很好的解释:
我有一个片段,我在 onActivityResult
从我的自定义画廊获取图像数据然后我需要更新我的 TextView
和 ImageView
在 onActivityResult
调用.我从 Whosebug 和 GitHub 尝试了几种方法,但 none 对我有用。我在日志中检查了我使用这样的路径成功获取图像 /storage/emulated/0/DCIM/Camera/IMG_20190430_221429.jpg
我还检查了它是否有效。
上传图片片段
public class UploadImage_Fragment extends Fragment implements View.OnClickListener {
public static final String TAG="###Upload Image###";
TextView uploadText;
ImageView uploadImage;
Button uploadButton;
App_Functions functions;
String cmpImage,Base64Image;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.upload_profile_cover_registration,container,false);
uploadText = v.findViewById(R.id.textViewImageUpload);
uploadImage = v.findViewById(R.id.uploadImage);
uploadButton = v.findViewById(R.id.uploadButton);
uploadButton.setEnabled(false);
Glide.with(getActivity()).load(R.drawable.cam_icon).into(uploadImage);
uploadImage.setOnClickListener(this);
uploadButton.setOnClickListener(this);
setHasOptionsMenu(true);
return v;
}
@Override
public void onClick(View view) {
int clickID=view.getId();
switch (clickID){
case R.id.uploadImage:
Media_Gallery gallery = new Media_Gallery();
gallery.setTargetFragment(this,101);
FragmentTransaction fragmentTrasaction=getFragmentManager().beginTransaction();
fragmentTrasaction.addToBackStack("Chat");
fragmentTrasaction.replace(R.id.FragmentLoginRegistration,gallery,"Chat");
fragmentTrasaction.commit();
break;
case R.id.uploadButton:
if (uploadButton.isEnabled()){
Log.d(TAG,"Button Is Enable Upload Image Here");
uploadButton.setEnabled(false);
}else {
Toast.makeText(getActivity(), "Select Image To Upload", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
functions=new App_Functions(getActivity());
if (requestCode==301 && resultCode== Activity.RESULT_OK){
Uri uri = data.getData();
cmpImage = functions.compressImage(uri,true);
Glide.with(getActivity()).load(cmpImage).apply(RequestOptions.circleCropTransform()).into(uploadImage);
uploadButton.setEnabled(true);
Log.d(TAG,"Image "+uri+" Compress Image "+cmpImage);
Bitmap bitmap = BitmapFactory.decodeFile(cmpImage);
Base64Image=functions.Convert_To_Base64(bitmap);
}else if (requestCode == 101 && resultCode == Activity.RESULT_OK){
ArrayList<String> imagePath = data.getStringArrayListExtra("Profile");
if (imagePath!=null){
String image = imagePath.get(0);
Log.d(TAG,"Image Path "+image);
uploadText.setText("Hey I am New Test"); // Not working
uploadImage.setImageDrawable(null);
uploadImage.invalidate();
File file = new File(image);
Uri newPath = Uri.fromFile(file);
Log.d(TAG,"Check File "+file);
Log.d(TAG,"Check Uri "+newPath);
Log.d(TAG,"Check ImageView "+uploadImage);
Glide.with(getActivity()).load(image).into(uploadImage); // Not working
}
}
}
@Override
public void onResume() {
super.onResume();
uploadImage.setImageDrawable(null);
uploadImage.invalidate();
}
}
我尝试了一些方法,例如使无效、设置图像 view.setBitmapImage(null) 但 none 对我也有效 我也尝试更新 textview 但它对我也不起作用。请帮助
您不应该直接从 onActivityResult
方法更新视图,因为它可以在您的 activity 即将被销毁和重新创建之前调用。
Tal Kanel对此提供了很好的解释: