Some questions about onActivityResult and startActivityForResult

Hello Folks,

Just started some android plugin tests. i have a couple of question i’m not able to solve right now.
Just wanna open the gallery and select an image, get the url back into unity.

  1. For some reason calling non-static functions just don’t work, no errors, just dont work:
public void GetImageFromGallery2() {
		debugLog("GIM 2");
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
	}
  1. onActivityResult is never called… ?? the static function is working (i get the image selector and am able to select an image)
    however there is no result…???
       public static void GetImagePathFromGallery() {
		debugLog2("GIM 3");
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		UnityPlayer.currentActivity.startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
		debugLog("VICTORY");
		if (resultCode == RESULT_OK) {
	                if (requestCode == 1) {
	                        // currImageURI is the global variable I'm using to hold the content:// URI of the image
	                        //currImageURI = data.getData();
	                	 UnityPlayer.UnitySendMessage("Main Camera", "SetImage", getRealPathFromURI(data.getData()));
	                	 
	               
	                }
	        }
	}

i have searched allot, some hits from people with the same problem. not found any suggestion what could help.

Best regards,
Martijn

Hey,

I am facing exactly same problem, is it solved?

Hi, I know that it is quite old topic, but recently I encountered exactly the same problem while i was developing my game.

I extended UnityPlayerActivity and wrote static function which I can call from Unity. It opens android gallery where I can pick photo, but then it comes back to Unity without calling onActivityResult method.

I override onActivityResult in order to return image path, but as I say it isn’t called.

Anyone has any solution for this problem, how can I call onActivityResult() method?

Edit:

A moment ago I’ve succeded called onActivityResult() and it works, but not completely. My game crashes after return from onActivityResult method. I I did this by this way:

public class OpenGallery extends UnityPlayerNativeActivity {

    private static final int REQUEST_CODE = 1;
    private String TAG = "opengallerytag";

    public static String imagePath = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, Uri.parse("content://media/internal/images/media"));

        startActivityForResult(intent, REQUEST_CODE);
    }

    public static void openGallery(Activity unityActivity) {

        Intent i = new Intent(unityActivity, OpenGallery.class);
        unityActivity.startActivity(i);

        /*Intent intent = new Intent(Intent.ACTION_GET_CONTENT, Uri.parse("content://media/internal/images/media"));

        unityActivity.startActivityForResult(intent, REQUEST_CODE);*/
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_CODE) {
            if(resultCode == RESULT_OK) {

                Uri imageUri = data.getData();
                String[] projection = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(imageUri, projection, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(projection[0]);

                imagePath = cursor.getString(columnIndex);
                cursor.close();
               
                UnityPlayer.UnitySendMessage("second_menu", "UpdateText", "filePath");
                finish();
            }
        }
    }
}

The Unity currentActivity call new activity which open Gallery. In this activity I override onActivityResult(), which update static field with path to image.

When UnityPlayerActivity finishes, it kills the process, so you won’t be able to get the activity result from Unity. This is done for a number of reasons.

However for separate activities (not the ones subclassed from UnityPlayerActivity) it should work just fine.

1 Like

Yeah, it works.