Search code examples
androidandroid-intentandroid-camera-intent

Android - take picture - onActivityResult returns immediately


In my App I would like to take a picture. The current Fragment is within a TabHost.

Within a fragment I start the camera action with:

Intent takePicture = new Intent( MediaStore.ACTION_IMAGE_CAPTURE);
getActivity().startActivityForResult( takePicture, 1);

In my MainActivity I have the onActivityResult:

@Override
public void onActivityResult( int requestCode, int resultCode, Intent imageReturnedIntent) {
       super.onActivityResult( requestCode, resultCode, imageReturnedIntent);
       if( imageReturnedIntent == null) { 
           // immediately coming here when my OWN app is singleTop or singleTask

When my App contains an Activity with launchmode singleTask or singleTop, then onActivityResult immediately returns with imageReturnedIntent is null. When I remove the launchmode in my App, then it works again.

How can I fix this?

What I read on the internet is that the activity that is launched (so, in my case the Camera App) should not have a launchmode singleTop or singleTask.

Question: how can my own Activity (having launchmode singleTop or singleTask) get an onActivityResult from a Camera App?

Notice: in Android 5.0+ this works fine. In Android 4.x not.


Solution

  • I do hope someone gives a better answer than this brute-force answer to starting the Camera App from your Activity that is launched with 'singleInstance' or 'singleTop'. So, I will remove this answer if anybody has a better solution.

    You can start a seperate activity - that will do the interfacing to the camera app. Yeah - I feel a bit ashamed devising this solution. In the end it works.

    OK, in the Manifest file you add:

    <activity  
        android:name  = "StartMedia" 
        android:label = "StarterMedia" 
        android:theme = "@android:style/Theme.NoDisplay" />
    

    The class contains this code --- and don't forget the finish():

    public class StartMedia extends Activity {
        @Override
        public void onCreate( Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Intent takePciture= new Intent( MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult( takePciture, 2);
        }
        @Override
        public void onActivityResult( int requestCode, int resultCode, Intent imageReturnedIntent) {
           if( imageReturnedIntent == null) { 
               Logger.v( "intent is null - action ...  ");
               return ; 
           }
           switch( requestCode) {
           case 1:
           case 2:
               Uri selectedImage = imageReturnedIntent.getData();
               String[] filePathColumn = { MediaStore.Images.Media.DATA};
               Cursor cursor = getContentResolver().query( selectedImage, filePathColumn, null, null, null);
               cursor.moveToFirst();
               int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
               String fileName = cursor.getString( columnIndex); 
               Logger.v( "Your Path:" + fileName);
               // DO something with the result
               cursor.close();
               break;
           }
           finish();  // important - otherwise it hangs your app
        }
    }
    

    Bottom line: there must be a better solution to this. If not, this may be your escape.