Search Unity

Some questions about onActivityResult and startActivityForResult

Discussion in 'Android' started by Pixelstudio_nl, Feb 19, 2014.

  1. Pixelstudio_nl

    Pixelstudio_nl

    Joined:
    Jun 22, 2009
    Posts:
    179
    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:
    Code (csharp):
    1.  
    2. public void GetImageFromGallery2() {
    3.         debugLog("GIM 2");
    4.         Intent intent = new Intent();
    5.         intent.setType("image/*");
    6.         intent.setAction(Intent.ACTION_GET_CONTENT);
    7.         startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
    8.     }
    9.  
    2) 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....???

    Code (csharp):
    1.  
    2.        public static void GetImagePathFromGallery() {
    3.         debugLog2("GIM 3");
    4.         Intent intent = new Intent();
    5.         intent.setType("image/*");
    6.         intent.setAction(Intent.ACTION_GET_CONTENT);
    7.         UnityPlayer.currentActivity.startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
    8.     }
    9.     @Override
    10.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    11.         debugLog("VICTORY");
    12.         if (resultCode == RESULT_OK) {
    13.                     if (requestCode == 1) {
    14.                             // currImageURI is the global variable I'm using to hold the content:// URI of the image
    15.                             //currImageURI = data.getData();
    16.                          UnityPlayer.UnitySendMessage("Main Camera", "SetImage", getRealPathFromURI(data.getData()));
    17.                          
    18.                    
    19.                     }
    20.             }
    21.     }
    22.  
    23.  
    i have searched allot, some hits from people with the same problem. not found any suggestion what could help.

    Best regards,
    Martijn
     
  2. DeveshPandey

    DeveshPandey

    Joined:
    Sep 30, 2012
    Posts:
    221
    Hey,

    I am facing exactly same problem, is it solved?
     
  3. Ostojski

    Ostojski

    Joined:
    Jan 21, 2016
    Posts:
    2
    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:

    Code (CSharp):
    1. public class OpenGallery extends UnityPlayerNativeActivity {
    2.  
    3.     private static final int REQUEST_CODE = 1;
    4.     private String TAG = "opengallerytag";
    5.  
    6.     public static String imagePath = null;
    7.  
    8.     @Override
    9.     protected void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.  
    12.         Intent intent = new Intent(Intent.ACTION_GET_CONTENT, Uri.parse("content://media/internal/images/media"));
    13.  
    14.         startActivityForResult(intent, REQUEST_CODE);
    15.     }
    16.  
    17.     public static void openGallery(Activity unityActivity) {
    18.  
    19.         Intent i = new Intent(unityActivity, OpenGallery.class);
    20.         unityActivity.startActivity(i);
    21.  
    22.         /*Intent intent = new Intent(Intent.ACTION_GET_CONTENT, Uri.parse("content://media/internal/images/media"));
    23.  
    24.         unityActivity.startActivityForResult(intent, REQUEST_CODE);*/
    25.     }
    26.  
    27.     @Override
    28.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
    29.         super.onActivityResult(requestCode, resultCode, data);
    30.         if(requestCode == REQUEST_CODE) {
    31.             if(resultCode == RESULT_OK) {
    32.  
    33.                 Uri imageUri = data.getData();
    34.                 String[] projection = {MediaStore.Images.Media.DATA};
    35.  
    36.                 Cursor cursor = getContentResolver().query(imageUri, projection, null, null, null);
    37.                 cursor.moveToFirst();
    38.                 int columnIndex = cursor.getColumnIndex(projection[0]);
    39.  
    40.                 imagePath = cursor.getString(columnIndex);
    41.                 cursor.close();
    42.                
    43.                 UnityPlayer.UnitySendMessage("second_menu", "UpdateText", "filePath");
    44.                 finish();
    45.             }
    46.         }
    47.     }
    48. }
    The Unity currentActivity call new activity which open Gallery. In this activity I override onActivityResult(), which update static field with path to image.
     
    Last edited: Jan 21, 2016
  4. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    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.
     
    Ostojski likes this.
  5. Ostojski

    Ostojski

    Joined:
    Jan 21, 2016
    Posts:
    2
    Yeah, it works.