Hello!

In my controller, when I click on the button, the selectImage() method is selectImage() . It works as onActivityResult(...) , but the onActivityResult(...) method is not called.

How to get images from the gallery in the controller?

 public class EditProfileController extends AppCompatActivity implements View.OnClickListener{ private int LOAD_IMAGE_RESULTS = 1; private static Activity activity; private static Context mContext; private static ViewHolder v; /** * EditProfileController This can be called at the start first time * @param activity Activity will transferred from onCreate in EditProfile.class */ public EditProfileController(Activity activity) { this((Context) activity); this.activity = activity; v = new ViewHolder(); } /** * EditProfileController This can be called when there is not activity available. * @param context Context will transferred from onCreate in EditProfile.class */ public EditProfileController(Context context){ mContext = context; } public void onCreate() { // set the view to activity activity.setContentView(R.layout.edit_profile_layout); // call to initView // @param activity initView(activity); } /** * initView called from onCreate and get the view from xml * @param view View that called from constractor */ public void initView(Activity view){ // Get the view v.imageView = (ImageView)view.findViewById(R.id.image_edit_profile); v.username = (TextView) view.findViewById(R.id.username); v.country = (TextView) view.findViewById(R.id.country_select); v.uploadImg =(TextView) view.findViewById(R.id.upload_image); v.save = (Button)view.findViewById(R.id.button_save); // call to method setListenner(); } /** * setListenner called from initView and set click listenner * @param */ public void setListenner(){ // set click listenner v.imageView.setOnClickListener(this); v.uploadImg.setOnClickListener(this); v.country.setOnClickListener(this); v.save.setOnClickListener(this); } /** * onClick * @param view View that called that onClickEvent */ @Override public void onClick(View view) { switch (view.getId()) { case R.id.button_save: EditProfileConstractor editProfileconst = new EditProfileConstractor(v.username.getText().toString(),v.username.getText().toString(),v.country.getText().toString()); break; case R.id.upload_image: selectImage(); break; case R.id.country_select: pupupCountryDialog(); break; default: break; } } /** * ViewHolder * @param */ private static class ViewHolder { // the view holder private ImageView imageView; private TextView username; private TextView country; private TextView uploadImg; private Button save; private String imagePath; } /** * pupupCountryDialog popup dialog with countries * @param */ private void pupupCountryDialog() { new MaterialDialog.Builder(mContext) .title(R.string.app_select_country) .items(R.array.countries_array) .itemsCallback(new MaterialDialog.ListCallback() { @Override public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) { v.country.setText(text); } }) .negativeText(R.string.app_cancel) .show(); } /** * selectImage start gallery image intent * @param */ private void selectImage() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); activity.startActivityForResult(intent, LOAD_IMAGE_RESULTS); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) { // Let's read picked image data - its URI Uri pickedImage = data.getData(); // Let's read picked image path using content resolver String[] filePath = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null); cursor.moveToFirst(); String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0])); // Now we need to set the GUI ImageView data with data read from the picked file. v.imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath)); // At the end remember to close the cursor or you will end with the RuntimeException! cursor.close(); } } } 
  • And the onActivityResult(...) method is in which class? And activity is an instance of which class? Show all the code. - post_zeew
  • please if you can tell me what needs to be changed so that the controller is as correct as possible - David Kern

1 answer 1

When you start the activation like this:

 activity.startActivityForResult(intent, LOAD_IMAGE_RESULTS); 

the result will come in the same activity , and not where you call it.

Generally speaking, you have some not very clear mess in the code.

If you inherit from AppCompatActivity , then try replacing this line:

 activity.startActivityForResult(intent, LOAD_IMAGE_RESULTS); 

on this one:

 startActivityForResult(intent, LOAD_IMAGE_RESULTS); 

Well, and this, in my understanding of the term controller , it should not be inherited from the class of activation.

  • one
    @DavidKern, On which line? - post_zeew 5:27
  • one
    @DavidKern; I can only advise you to at least divide the controller and activations into two separate classes. - post_zeew
  • one
    @DavidKern, you don’t need to call super at all. Try to delete it simply - YuriySPb
  • one
    @DavidKern, The result must be obtained not in the controller, but in activation. - post_zeew
  • one
    @DavidKern; Add a method to the controller that will receive the image and perform the necessary manipulations on it, and then call this method from the activit. - post_zeew 5:43