Question

I have an activity that allows the user to select preview a photo that they select from the gallery or the camera. The problem I'm having is that the camera/gallery intent returns immediately, then shows the camera/gallery and returns nothing.

The basic flow of things is as follows: Fragment -> Application Subclass -> Top Activity -(startActivity)-> Photo Preview Activity -(in onCreate)-> Photo Chooser Intent


//In the application subclass
public static void launchImageSelector()
{
    if(!(topActivity instanceof ImagePreviewActivity))
    {
        Intent i = new Intent(context, ImagePreviewActivity.class);
        topActivity.startActivityForResult(i, kImageSelectorRequestCode);
    }
}

///in ImagePreviewActivity class
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    Intent chooser = createChooserIntent(createCameraIntent());
    chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent("image/*"));
    startActivityForResult(chooser, 1);
}

//intent creaters(from android src)
private Intent createChooserIntent(Intent... intents)
{
    Intent chooser = new Intent(Intent.ACTION_CHOOSER);
    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);
    chooser.putExtra(Intent.EXTRA_TITLE, "Choose Photo");
    return chooser;
}

private Intent createOpenableIntent(String type) 
{
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
//      i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType(type);
    return i;
}

private Intent createCameraIntent() 
{
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File externalDataDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DCIM);
    File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
            File.separator + "browser-photos");
    cameraDataDir.mkdirs();
    String mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
            System.currentTimeMillis() + ".jpg";

    photoFileUri = Uri.fromFile(new File(mCameraFilePath));

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFileUri);

    return cameraIntent;
}

What am I doing wrong here? What would cause the Chooser Intent to return immediately but also continue? Am I doing something fundamentally wrong here?

Thanks for the help!!

Was it helpful?

Solution

After hours of debugging the problem was in the Manifest file. In android, you can't start an activity for a result if the launch mode is set to singleInstance or singleTop

Found the answer here: Android - startActivityForResult immediately triggering onActivityResult

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top