Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Need help getting video from Gallery - Xamarin.Android

Hello everyone.
I am working on a project in Xamarin.Android and I need to be able to get a video from the gallery and send it to a web service so it can be saved.

I am very close but I just need to know how to actually read the path to the file and send it.

Here is what I have to pull the gallery:

    Intent = new Intent();
        Intent.SetType("video/*");
        Intent.SetAction(Intent.ActionGetContent);
        StartActivityForResult(Intent.CreateChooser(Intent, "Select Video"), PickImageId);

Once that is done, it pulls up the gallery and lets me select the video.

I then use this to pull the video:

        async protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
            {
                try
                {
                        uri_G = data.Data;
                        Task mytask = new Task(delegate { SelectPhoto(requestCode, resultCode, data); });
                        mytask.Start();
                        await mytask;
                        Android.Widget.Toast.MakeText(this, "Video is ready for upload...", Android.Widget.ToastLength.Short).Show();

                }
                catch (Exception ex)
                {
                    CM.AlertDialog(this, "Error", "An error occurred while processing your request. Please contact IT support with this error: " + ex.Message, "OK");
                }
            }

        }


        private void SelectPhoto(int requestCode, Result resultCode, Intent data)
        {
            RunOnUiThread(Action);
        }

        private void Action()
        {
            Android.Net.Uri uri = uri_G;

            using (MemoryStream stream = new MemoryStream())
            {           
                    var ByteArray = stream.ToArray();
                    string strBase64SmallBMP = Convert.ToBase64String(ByteArray);
                    Common.VideoString = strBase64SmallBMP;
                    stream.Close();
                    stream.Dispose();

                    //popup a message saying it was saved.

                    StartActivity(typeof(InspectionEnd));

            }
        }

Can someone tell me how I can send the video to the stream object? When I debug, the stream object is empty.....
If I can get it to the stream, I will be able to handle it however.

Thanks!!

Best Answers

Answers

  • asusmaniasusmani Member ✭✭
    edited October 14

    It got to the getVideoPath method and failed at line:

    var cursor = this.ContentResolver.Query(uri, null, selection, null, null);
    
    

    Error message says:

    Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/video/media from pid=15311,
    uid=10224 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()'

    My manifest shows that is present.

    Help?

  • asusmaniasusmani Member ✭✭

    Alright I got it working!
    Thanks for both of your help.

    Here is the code I used in order to request the permissions on runtime in case someone else needs help.
    I just call this method in my OnCreate().

            void RequestCameraPermission()
            {
                Log.Info("VideoUpload", "STORAGE permission has NOT been granted. Requesting permission.");
    
                if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.ReadExternalStorage))
                {
                    // Provide an additional rationale to the user if the permission was not granted
                    // and the user would benefit from additional context for the use of the permission.
                    // For example if the user has previously denied the permission.
                    Log.Info("VideoUpload", "Displaying camera permission rationale to provide additional context.");
                    Android.Widget.Toast.MakeText(this, "Need permissions rationale", Android.Widget.ToastLength.Short).Show();
                }
                else
                {
                    // Camera permission has not been granted yet. Request it directly.
                    ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadExternalStorage }, 0);
                }
            }
            public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
            {
                if (requestCode == 0)
                {
                    // Received permission result for camera permission.
                    Log.Info("VideoUpload", "Received response for Storage permission request.");
    
                    // Check if the only required permission has been granted
                    if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
                    {
                        // Camera permission has been granted, preview can be displayed
                        Log.Info("VideoUpload", "Storage permission has now been granted. Showing preview.");
                        Android.Widget.Toast.MakeText(this, "Storage access granted", Android.Widget.ToastLength.Short).Show();
                    }
                    else
                    {
                        Log.Info("VideoUpload", "Storage permission was NOT granted.");
                        Android.Widget.Toast.MakeText(this, "Storage access NOT granted", Android.Widget.ToastLength.Short).Show();
                    }
                }
                else
                {
                    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
                }
            }
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    Forgot to mention we need to dynamically request the external permissions as what @Christ said.
    If your issue has been solved, could you please mark the reply as an answer?

  • asusmaniasusmani Member ✭✭
    Yep. I have marked both responses as answers.

    Thanks guys.
Sign In or Register to comment.