David Abaev
David Abaev

Reputation: 1

Xamarin - IOS - Video assets in Photo library - Find By name

I'm new in Xamarin development.

I build my app, where user can clicks on DOWNLOAD button. This button download video from the server and save to Photo library.

Here is how I implement this (maybe its incorrect way??)

public bool SaveVideo(byte[] videoData, int id)
    {
        try
        {

                CreateCustomAlbum();


            //  Save file to applicaiton folder
            string local_path = SaveFileToApplicationFolder(videoData);

            _lib.WriteVideoToSavedPhotosAlbum(new Foundation.NSUrl(local_path), (t, u) =>
            {
                DeleteLocalFile(local_path); // HERE I DELETE FILE FOR NOT INCREASE SIZE OF APPLICATION


                _local_file_path = t.AbsoluteUrl.ToString(); // global variable 

                _lib.Enumerate(ALAssetsGroupType.Album, HandleALAssetsLibraryGroupsEnumerationResultsDelegate, (obj) => { });
            });
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }


    void DeleteLocalFile(string local_path)
    {

        try
        {
            if (File.Exists(local_path))
            {
                File.Delete(local_path);
                if (!File.Exists(local_path))
                {
                    Console.WriteLine("Deleted");
                }
            }
        }
        catch (Exception ex)
        {

        }
    }

string SaveFileToApplicationFolder(byte[] videoData)
    {
        try
        {
            string file_path = String.Empty;



            var doc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string filename = "MY-APP-" + id + ".mp4"; // id global variable
            file_path = Path.Combine(doc, filename); // global variable

            File.WriteAllBytes(file_path, videoData);

            return file_path;
        }
        catch (Exception ex)
        {
            return String.Empty;
        }
    }

void HandleALAssetsLibraryGroupsEnumerationResultsDelegate(ALAssetsGroup group, ref bool stop)
    {
        try
        {
            if (group == null)
            {
                stop = true;
                return;
            }
            if (group.Name == "MY-APP-ALBUM-NAME")
            {
                stop = true;
                _current_album = group;
                SaveFileToCustomAlbum();
            }
        }
        catch (Exception ex)
        {

        }
    }


void SaveFileToCustomAlbum()
        {
            try
            {
                if (_current_album != null && !String.IsNullOrEmpty(_local_file_path))
                {
                    _lib.AssetForUrl(new Foundation.NSUrl(_local_file_path), delegate (ALAsset asset)
                    {
                        if (asset != null)
                        {

                            _current_album.AddAsset(asset);
                        }
                        else
                        {
                            Console.WriteLine("ASSET == NULL.");
                        }
                    }, delegate (NSError assetError)
                    {
                        Console.WriteLine(assetError.ToString());
                    });
                }
            }
            catch (Exception ex)
            {

            }
        }

So this code do: 1) Save video to local folder my video - Method SaveFileToApplicationFolder 2) Then Save video file to Photo library - Method SaveVideo 3) Then Delete file from app folder (in purpose not increase application folder size (app size) --- IF ITS CORRECT logic?? 4) Then put assets to Custom Album for my App

SO everything here works well for me......BUT! I need overtime when user open item - check if he already has video for this item in photos library or not?

And here I'm stack....i just don't understand how i can to check if user has specific video?? I don't find hot to set NAME for ASSETS and hot looking for assets by name...so don't know hot to find this assets....METADATA?? Key_VALUE of object??

Upvotes: 0

Views: 599

Answers (2)

DavidShepard
DavidShepard

Reputation: 296

You may just want to use xam.plugin.media nuget package. It makes it very easy to take and store videos as well as access the default video picker for selecting existing videos

Upvotes: 0

ColeX
ColeX

Reputation: 14475

Refer to Obj-C Check if image already exists in Photos gallery

In short:

  1. Store assetUrl when saving video with NSUserDefaults

  2. Check if the video exists in Photo library with assetUrl when next time to open it .

Upvotes: 0

Related Questions