I want to diplay an image (selected from gallery) in a fragment. But when I return the onActivityResult is called but the fragment is blank, both methods (loadImageFromGallery and startActivityFromResult are in MainActivity, which host the fragment)
The MainActivity. java
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment;
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_agregarComida) {
setTitle(R.string.TitleAddFood);
fragment = new AddFood();
Bundle args = new Bundle();
args.putInt(AddFood.ARG_ADD_FOOD, 0);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
}
The onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
// Set the Image in ImageView after decoding the String
imageViewProfile.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, getResources().getString(R.string.NoPickPhoto) , Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, getResources().getString(R.string.PickPhotoException) , Toast.LENGTH_LONG).show();
}
}
The loadImageGallery
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
My class AddFood which extends Fragment
public class AddFood extends Fragment {
public static final String ARG_ADD_FOOD = "arg_add_food";
public RatingBar ratingBar;
public EditText NombreComidaEditText;
public AddFood() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_add_food, container, false);
//findViewById
ratingBar = (RatingBar)rootView.findViewById(R.id.ratingBar);
NombreComidaEditText = (EditText) rootView.findViewById(R.id.NombreComidaEditText);
ratingBar.setNumStars(5);
Toast.makeText(getActivity(),"onCreateViewAddFood",Toast.LENGTH_LONG).show();
return rootView;
}
}
And the xml for the ImageView
<ImageView
android:id="@+id/photoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/photoTextView"
android:src="@drawable/ic_action_camera"
android:layout_centerHorizontal="true"
android:onClick="loadImagefromGallery"/>