Home > Mobile >  Xamarin Forms Android 10 and higher find video by filename in public external storage and get path
Xamarin Forms Android 10 and higher find video by filename in public external storage and get path

Time:10-12

im developing a Xamarin Forms App where the User can take Videos and save them to the public external storage. Im currently saving the filename of the created video. My way of saving the video:

  private readonly string DirectoryName = "KiloFürKilo";

  public async Task<string> CaptureVideoAsync()
        {
            var photo = await MediaPicker.CaptureVideoAsync();
            await using var stream = await photo.OpenReadAsync();
            await using var memoryStream = new MemoryStream();
            await stream.CopyToAsync(memoryStream);
            var filename = "KforK"   DateTime.Now   ".mp4";
            SaveVideoFromByte(memoryStream.ToArray(), filename);
            return filename;
        }

 private async void SaveVideoFromByte(byte[] imageByte, string filename)
        {
            var context = CrossCurrentActivity.Current.AppContext;
            var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
            //Android 10 
            if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.P)
            {
                using var resolver = context.ContentResolver;
                var contentValues = new ContentValues();
                contentValues.Put(MediaStore.IMediaColumns.DisplayName, filename);
                contentValues.Put(MediaStore.IMediaColumns.MimeType, "video/mp4");
                contentValues.Put(MediaStore.IMediaColumns.RelativePath, "DCIM/"   DirectoryName);


                var uri = resolver.Insert(MediaStore.Video.Media.ExternalContentUri, contentValues);

                using var stream = resolver.OpenOutputStream(uri);
                await stream.WriteAsync(imageByte);
                stream.Close();
                mediaScanIntent.SetData(uri);
            }
            else
            {
                var rootPath =
                    Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMovies);
                var storagePath = Path.Combine(rootPath.ToString(), DirectoryName);
                if (!File.Exists(storagePath))
                {
                    Directory.CreateDirectory(storagePath);
                }

                string path = Path.Combine(storagePath.ToString(), filename);
                File.WriteAllBytes(path, imageByte);

                mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(path)));
            }

            context.SendBroadcast(mediaScanIntent);
        }

I now want to be able to open and play the video, without the user need to pick it from the gallery. How can i find a video in the external public storage by a filename and retrieve the path to it? I think i have to use the MediaStore but could not figure out how. Thanks to anyone who can help me with this problem:)

CodePudding user response:

MediaStore insert() gave you an uri where you wrote the file.

To read/play the file you can use the same uri.

Further your relative path was "DCIM/" DirectoryName;

And if you need a path then complete path is "/storage/emulated/0/DCIM/" DirectoryName "/" filename.

  • Related