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(); } } }
onActivityResult(...)method is in which class? Andactivityis an instance of which class? Show all the code. - post_zeew