The activity hosting this fragment has its onActivityResult
called when the camera activity returns.
My fragment starts an activity for a result with th
Inside your fragment, call
this.startActivityForResult(intent, REQUEST_CODE);
where this
is referring to the fragment. Otherwise do as @Clevester said:
Fragment fragment = this;
....
fragment.startActivityForResult(intent, REQUEST_CODE);
I also had to call
super.onActivityResult(requestCode, resultCode, data);
in the parent activity's onActivityResult
to make it work.
(I adapted this answer from @Clevester's answer.)
Solution 1:
Call startActivityForResult(intent, REQUEST_CODE);
instead of getActivity().startActivityForResult(intent, REQUEST_CODE);
.
Solution 2:
When startActivityForResult(intent, REQUEST_CODE);
is called the activity's onActivityResult(requestCode,resultcode,intent)
is invoked, and then you can call fragments onActivityResult()
from here, passing the requestCode, resultCode and intent
.
Most of these answers keep saying that you have to call super.onActivityResult(...)
in your host Activity
for your Fragment
. But that did not seem to be working for me.
So, in your host Activity
you should call your Fragments
onActivityResult(...)
instead. Here is an example.
public class HostActivity extends Activity {
private MyFragment myFragment;
protected void onActivityResult(...) {
super.onActivityResult(...);
this.myFragment.onActivityResult(...);
}
}
At some point in your HostActivity
you will need to assign this.myFragment
the Fragment
you are using. Or, use the FragmentManager
to get the Fragment
instead of keeping a reference to it in your HostActivity
. Also, check for null
before you try to call the this.myFragment.onActivityResult(...);
.
public class takeimage extends Fragment {
private Uri mImageCaptureUri;
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_FILE = 2;
private String mPath;
private ImageView mImageView;
Bitmap bitmap = null;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_send_image, container, false);
final String[] items = new String[] { "From Camera", "From SD Card" };
mImageView = (ImageView)view.findViewById(R.id.iv_pic);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_item, items);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select Image");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "tmp_avatar_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg");
mImageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(
android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
intent.putExtra("return-data", true);
getActivity().startActivityForResult(intent,
PICK_FROM_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}
dialog.cancel();
} else {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getActivity().startActivityForResult(
Intent.createChooser(intent,
"Complete action using"), PICK_FROM_FILE);
}
}
});
final AlertDialog dialog = builder.create();
Button show = (Button) view.findViewById(R.id.btn_choose);
show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Switch the tab content to display the list view.
dialog.show();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK)
return;
if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
// mPath = getRealPathFromURI(mImageCaptureUri); //from Gallery
if (mPath == null)
mPath = mImageCaptureUri.getPath(); // from File Manager
if (mPath != null)
bitmap = BitmapFactory.decodeFile(mPath);
} else {
mPath = mImageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(mPath);
}
mImageView.setImageBitmap(bitmap);
}
public String getRealPathFromURI(Uri contentUri) {
String [] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, proj, null, null,null);
if (cursor == null) return null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
ADD this
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
}
when you will replace your code with this above code then automatically your this
public void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data){}
Method will Start working
//No Need to write this code in onclick method
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(intent,1);
Toast.makeText(getContext(), "image"+intent, Toast.LENGTH_SHORT).show();
In my case it was an Android bug (http://technet.weblineindia.com/mobile/onactivityresult-not-getting-called-in-nested-fragments-android/), if you use supported FragmentActivity
you have to use getSupportFragmentManager
instead of getChildFragmentManager
:
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
if(fragment instanceof UserProfileFragment) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}