I want the user to select an image from the image gallery and the app will go into another activity and load the image there. This is to open the file chooser.
private void openFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getActivity().startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
This is what i have in my onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
mImageUri = data.getData();
Intent intent = new Intent(getActivity(),AddActivity.class);
intent.putExtra("imagePath",mImageUri.toString());
intent.putExtra("requestCode",PICK_IMAGE_REQUEST);
startActivity(intent);
}
This is my code for the second activity, basically the user can either open the camera and take a photo or choose from an image gallery. The taking photo from camera works but the choosing image does not thats why i have two request codes.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_additem);
Log.i(TAG, "onCreate: started");
setupBottomNavigationView();
imageView = findViewById(R.id.addPic);
Bundle extras = getIntent().getExtras();
int requestCode = extras.getInt("requestCode");
if (requestCode == 0) {
if (extras != null) {
Bitmap image = (Bitmap) extras.get("image");
if (image != null) {
imageView.setImageBitmap(image);
}
}
if (requestCode == 1) {
Intent intent = getIntent();
String image_path = intent.getStringExtra("imagePath");
Uri fileUri = Uri.parse(image_path);
imageView.setImageURI(fileUri);
}
}
}
Been unable to understand why it does not work, my android app does not even go to the second activity after choosing the image. It just returns to the home activity.