Get image from gallery to set image in fragment?

How to get image from gallery to set image in fragment? onActivityResult is not called in the fragment .. this code I wrote in the fragment

img_user.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); } }); public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (data != null) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); img_user.setImageBitmap(BitmapFactory.decodeFile(picturePath)); cursor.close`enter code here`(); } else { Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT) .show(); } } 
+5
source share
3 answers

You can do it this way .... in your main action

  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //call super super.onActivityResult(requestCode, resultCode, data); } 

and inside your fragment

  Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); // ******** code for crop image i.putExtra("crop", "true"); i.putExtra("aspectX", 100); i.putExtra("aspectY", 100); i.putExtra("outputX", 256); i.putExtra("outputY", 356); try { i.putExtra("return-data", true); startActivityForResult( Intent.createChooser(i, "Select Picture"), 0); }catch (ActivityNotFoundException ex){ ex.printStackTrace(); } 

and

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==0 && resultCode == Activity.RESULT_OK){ try { Bundle bundle = data.getExtras(); Bitmap bitmap = bundle.getParcelable("data"); img_user.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } } } 

Great for me

+3
source

I also had a problem. In fact, the callback for startActivityForResult() returns to the parent activity, so you need to make an explicit call from the fragment to the onActivityResult() function as follows.

In the Parent Activity class, override the onActivityResult() method and even override it in the fragment class and call it as the following code.

In the parent class:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Fragment fragment = getFragmentManager().findFragmentById(R.id.container); fragment.onActivityResult(requestCode, resultCode, data); } 

In the kids class: This Extends snippet follows your logic for adjusting the image in image view.

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (data != null) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); image.setImageBitmap(BitmapFactory.decodeFile(picturePath)); cursor.close(); } else { Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT).show(); } super.onActivityResult(requestCode, resultCode, data); } 
+1
source

Please find the following code, it will work. Add this to your snippet

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { //Your stuff } 
0
source

Source: https://habr.com/ru/post/1237651/


All Articles