I am working on some Android app with single Activity, named MainActivity and several Fragments. Now, through application usage at some point the application fetches images from target device local storage and shows them for selection in PhotoPickerFragment, like shown in following screenshot:
PhotoPickerFragment also has its PhotoPickerAdapter for showing contents in GridView. To transfer selected image between Fragments I have written following chunk of code inside PhotoPickerAdapter's ArrayAdapter getView() method :
public View getView(int position,
@Nullable View convertView,
@NonNull ViewGroup parent)
{
PhotoPickerRecord photoPickerRecord=getItem(position);
if(convertView==null)
{
LayoutInflater inflater=(LayoutInflater)parent.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.layout_photo_picker_delegate,
parent,
false);
}
ImageView ivPhotoPickerDelegate=convertView.findViewById(R.id.ivPhotoPickerDelegate);
ivPhotoPickerDelegate.setImageBitmap(photoPickerRecord.scaledPhoto);
ivPhotoPickerDelegate.setOnClickListener(v->
{
MainActivity mainActivity=(MainActivity)getContext();
NewPlantFragment newPlantFragment=(NewPlantFragment)mainActivity.getNewPlantFragment();
Bundle selectedRecordBundle=new Bundle();
ByteArrayOutputStream selectedPhotoStream=new ByteArrayOutputStream();
String encodedSelectedPhoto=new String();
selectedRecordBundle.putString(BundleKeys.SELECTED_PHOTO_URI_KEY,
photoPickerRecord.uri.toString());
photoPickerRecord.scaledPhoto.compress(Bitmap.CompressFormat.PNG,
100,
selectedPhotoStream);
encodedSelectedPhoto=Base64.encodeToString(selectedPhotoStream.toByteArray(),
Base64.DEFAULT);
selectedRecordBundle.putString(BundleKeys.SELECTED_PHOTO_THUMBNAIL_KEY,
encodedSelectedPhoto);
newPlantFragment.setArguments(selectedRecordBundle);
mainActivity.switchToScreen(newPlantFragment);
});
return convertView;
}
Selected image is, as you can see, packed into Bundle and then Fragment switch (from PhotoPickerFragment to NewPlantFragment) occurs. Target NewPlantFragment has declared target ImageButton in it (red background is for test purposes only, will be removed):
as you can also see from screenshot:
Now, after fragment switcing, NewPlantFragment.OnResume() is called:
@Override
public void onResume()
{
super.onResume();
this.updatePlantPhoto(this.getArguments());
}
and them we apply transfered image to ibPhoto ImageButton in updatePlantPhoto method:
private void updatePlantPhoto(Bundle plantPhotoInfoBundle)
{
if((plantPhotoInfoBundle!=null)&&
(plantPhotoInfoBundle.size()>0))
{
if(plantPhotoInfoBundle.containsKey(BundleKeys.SELECTED_PHOTO_URI_KEY)&&
(plantPhotoInfoBundle.containsKey(BundleKeys.SELECTED_PHOTO_THUMBNAIL_KEY)))
{
byte[] selectedPlantPhotoByteArray=Base64.decode(plantPhotoInfoBundle.getString(BundleKeys.SELECTED_PHOTO_THUMBNAIL_KEY),
Base64.DEFAULT);
Bitmap reconstructedSelectedPlantPhoto=BitmapFactory.decodeByteArray(selectedPlantPhotoByteArray,
0,
selectedPlantPhotoByteArray.length);
this.imgBtnPhoto.setImageBitmap(reconstructedSelectedPlantPhoto);
plantPhotoInfoBundle.clear();
this.setArguments(plantPhotoInfoBundle);
}
}
}
However, the ibPhoto ImageButton is not updated, there is still red rectangle with no image visible as you can se from screenshot:
Now, I've been trying to debug this problem and in debug process, I can see the selected image is decoded correctly with help of Android Studio, I can see decoded Image inside debugger. Why the ImageButton's image is not updated?
UPDATE 1
On request on @JornRigter, I am sending screenshot of debug situation with breakpoint:
As you can see, selected image is decoded.