0

i am trying to open image intent (select files from gallery) but need to pass certain id (project / report id) which needs to be processed for intent result.

Whatever i have tried for intent options / flags, putting extra for intent (string) is working, but on result extras are empty always. this is the related code (both in mainactivity):

var imageIntent = new Intent(Intent.ActionPick);
                    imageIntent.SetType("image/*");
                    imageIntent.PutExtra(Intent.ExtraAllowMultiple, true);
                    imageIntent.SetAction(Intent.ActionGetContent);
                    imageIntent.AddFlags (ActivityFlags.GrantReadUriPermission);

                    if (ProjectId != null && ProjectId.ToString().Length > 0) imageIntent.PutExtra("ProjectId", ProjectId.ToString());
                    if (ReportId != null && ReportId.ToString().Length > 0) imageIntent.PutExtra("ReportId", ReportId.ToString());

                    CrossCurrentActivity.Current.Activity.StartActivityForResult(imageIntent, requestCode);

and

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

        ...

        if (data.Extras != null)
        {
          if (data.GetStringExtra ("ProjectId") != null) int.TryParse(data.GetStringExtra("ProjectId"), out ProjectId);
          if (data.GetStringExtra("ReportId") != null) Guid.TryParse(data.GetStringExtra("ReportId"), out ReportId);
        }

data.Extras is null here.

Any suggestions?

Thanks in advance..

Norman
  • 33
  • 1
  • 3

1 Answers1

1

data.Extras is null

It is correct, there are two Intents.

When you use StartActivityForResult to open the Gallery, this imageIntent we can called Intent1, Intent1 contains ProjectId and ReportId. When you select the Image, back to the previous activity, System will generate a new Intent, we can called Intent2. Itent2 just contains you select image information, it not contains ProjectId and ReportId, So you get null with data.Extras in OnActivityResult.

If you want to get the ProjectId and ReportId information in any activity or other classes in android project, you can use SharedPreferences to achieve it.

How do I use SharedPreferences in Xamarin.Android?

Leon
  • 8,404
  • 2
  • 9
  • 52
  • would not like to handle this by SharedPreferences, as requirement here is to avoid issues with gallery / messagingservice (which are leading to incorrect project / report id). any other to handle / achieve that? – Norman Aug 25 '20 at 09:57
  • In xamarin forms, you can use `File Handling` or `local Databases` to store information. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/databases – Leon Aug 25 '20 at 10:32
  • @NormanNor Are there any update for this issue, if above answer is helpful, please do not forget to accept it as answer(click the “✔” in the upper left corner of this answer), it will help others who have similar issue – Leon Sep 04 '20 at 07:26