I'm trying to take a picture in gallery, so i know do this in Activity, i use a Intent to call the gallery, and onActivityResult for take the path, but when i use a Fragment, i cannot to use "onActivityResult", can someone give a example of it using a Fragment and CustomDialog?
Asked
Active
Viewed 171 times
-2
-
*but when i use a Fragment, i cannot to use "onActivityResult"* ... why? – Selvin Nov 21 '16 at 10:27
-
Hi, try to this link http://stackoverflow.com/questions/25759227/how-to-display-image-in-imageview-from-gallery-in-fragment – Dileep Patel Nov 21 '16 at 10:32
-
1https://developer.android.com/reference/android/app/Fragment.html#onActivityResult(int,int,android.content.Intent) Here you go.It's available in Fragment.. – Sunil Sunny Nov 21 '16 at 10:34
-
There is no correct answer here... The question don't need it (take a look at my comment and sunil's comment and you will get a correct answer) – Selvin Nov 23 '16 at 09:21
1 Answers
1
Inside your fragment write this code
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();
}
In you Main Activity of the fragment write this code onActivityResult
@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();
}
}
}
Try with this, it should work. Don't forget to accept the answer if correct.

Prashant Sable
- 1,003
- 7
- 19
-
*In you Main Activity of the fragment* ???? fragment does not contain Activity ... also what if multiple Activities use this fragment? – Selvin Nov 21 '16 at 10:28
-
Every Fragment is having its own Parent Activity. You have to write this method in the Activity from where you have initialized your fragment. – Prashant Sable Nov 21 '16 at 10:32
-