Note that there are some explanatory texts on larger screens.

plurals
  1. POonActivityResult not working with RESULT_OK, RESULT_CANCEL, etc when using camera on Android
    text
    copied!<p>I've got in my class something like this:</p> <pre><code> public class Main extends Activity { private static final int CAMERA_PICK = 1; private static final int GALLERY_PICK = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button photo = (Button) findViewById(R.id.button); photo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { createDialog(); } }); private void createdialog(Activity activity) { final CharSequence[] items = { "Take shot", "Take from gallery" }; AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("Get image"); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); if (item == 0) { takePhoto(); } if (item == 1) { choosePhoto(); } } }); AlertDialog alert = builder.create(); alert.show(); } protected void choosePhoto() { // not necessary; } protected void takePhoto() { Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "myPic" + String.valueOf(System.currentTimeMillis()) + ".jpg")); cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri); try { cameraIntent.putExtra("return-data", true); startActivityForResult(cameraIntent, CAMERA_PICK); //Doing something with the picture here; } } catch (Exception e) { e.printStackTrace(); } } // TODO @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { case CAMERA_PICK: break; case RESULT_OK: Toast.makeText(Main.this, "Photo selected", Toast.LENGTH_SHORT).show(); break; } } } </code></pre> <p>As you can see I am trying to use the phone's camera to take a picture and use it later on an image view. The thing is there I can't trigger the method <i>onActivityResult(...)</i>! When I do the startActivityForResult(cameraIntent, CAMERA_PICK); I do not have a way to manipulate the RESULT_OK, RESULT_CANCEL or even the one I defined as CAMERA_PICK. The onActivityResult(...) should work on perfection by I am not understand what I'm doing wrong!</p> <p>Any help would be aprecciated, Thanks.</p> <blockquote> <p>I've already found the problem. I was using an activity group and I didn't realized that the onActivityResult() triggered was the first one of all the activities...</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload