2 years ago

#56584

test-img

Dong Li

How do I open a the folder where an image is located from my Xamarin Android Application?

I have tried looking almost everywhere on how to open the parent folder of a child image displayed in my app with no success. Am debugging my Android Application to a device running Android API level 29. The inbuilt samsung File Manager has a menu item for navigating to the source folder of any file and that is why I believe there is a way to tell the App to open the location of the image displayed on my ImageSwitcher object. The app has a context menu with a Open Source Folder menu item for opening the source folder of the image, the code to do that is the problem...

Code

 public override bool OnContextItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
             case Resource.Id.image_folder:
                    try
                    {
                       //I tried this inbuild dotnet method but it does not work
                        string file_path = new Java.IO.File(files[index]).AbsolutePath;
                        DirectoryInfo info= new DirectoryInfo(file_path);
                        info.MoveTo(new Java.IO.File(file_path).Parent);
                        
                    }catch(Exception ex)
                    {
                        Android.App.AlertDialog.Builder mybuilder= new Android.App.AlertDialog.Builder(this);
                        mybuilder.SetTitle("Exception");
                        mybuilder.SetMessage(ex.Message);
                        mybuilder.Show();
                    }
                    break;
            }
         }

Code below shows how I obtained the path to the ImageSwicther currentl image.

 void read_images()
        {
            //access the application's directory
            Java.IO.File original_file = (Application.GetExternalFilesDir(null));

            //check if the file is read only
            if (original_file.CanRead())
            {
                //Toast.MakeText(this, "This folder can be read", ToastLength.Long).Show();
            }
            if (original_file.Exists() && original_file.IsDirectory)
            {
                try
                {
                    //make sure the file is an image file
                    //apply the code for image mime type extensions
                    
                   files = Directory.GetFiles(original_file.AbsolutePath);
                    for (int i = 0; i < files.Length; i++)
                    {
                        //delete any file that does not end with an image format
                        if (files[i].EndsWith("jpg") == false || files[i].EndsWith("png") == false)
                        {
                            Directory.Delete(files[i]);
                        }
                    }

                }
                catch (Exception e)
                {
                    Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this);
                    alert.SetTitle("File Read Exception");
                    alert.SetMessage(e.Message + "\n" + e.Data);
                    alert.Show();
                }
                // Toast.MakeText(this, "File exists",ToastLength.Short).Show();
            }
        }

Now that the I have the paths to the images in a string array, all I do is use an integer variable called index to switch the path and display it to the ImageSwitcher object when user swipes right or left like

                               //increment index for right swipe and decrement for left 
                                myswitcher.ClearAnimation();
                                myswitcher.SetInAnimation(this,Resource.Animation.slide_in_left);
                                myswitcher.SetOutAnimation(this,Resource.Animation.slide_out_left);
                                myswitcher.SetImageURI(Android.Net.Uri.FromFile(new Java.IO.File(files[index])));                           

NB: The files[index] is a string array containing paths of all the images displayed in the imageswitcher object.

android

directory

contextmenu

0 Answers

Your Answer

Accepted video resources