tm1701
tm1701

Reputation: 7581

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.

Upvotes: 3

Views: 1811

Answers (2)

babay
babay

Reputation: 4844

The problem is:

A "singleInstance" activity, on the other hand, permits no other activities to be part of its task.

see here https://developer.android.com/guide/topics/manifest/activity-element.html#lmode

and the startActivityForResult launches new activity in the same task.

So, if a task is started for an activity in singleInstance mode, then Android is unable to launch new activity in the task.

You should use singleTask mode instead. Is is much like singleInstance, but allows other activities to be launched on the task.

on android >=5.0 you will have another problem with singleInstance mode: you always have resultCode == Activity.RESULT_CANCELED.

Upvotes: 1

tm1701
tm1701

Reputation: 7581

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.

Upvotes: 0

Related Questions