locked
Retrieve all photos from custom folder on Android RRS feed

  • Question

  • User396123 posted

    On my Android device, inside of the Files folder under Images, I've created a new folder 'MyFolder' and added some photos.

    I need to access these images without using a picker and put them into a list

    I've looked at many documentation on file access, but I can't find anything that shows how to target a specific folder by name and retrieve its contents.

    Can someone point me in the direction of some documentation or provide some sample code to help me understand this please

    Friday, August 7, 2020 10:21 AM

Answers

  • User379860 posted

    You can write a dependenceService to achieve it.

    First of all create an interface in the PCL. public interface IFile { List<byte[]> readImages(); } Then achieve it in the android, Notice, I do not create a foler called MyFolder under Images folder, I have temp folder under Images folder, I use following code to read all of images to Byte[] ``` [assembly: Dependency(typeof(ReadImages))] namespace App15.Droid { public class ReadImages:IFile { public List readImages() { List imagePaths = GetAllImagePath();

            List<byte[]> allImages = new List<byte[]>();
            byte[] cachedBytes;
            foreach (var path in imagePaths)
            {
    
    
               cachedBytes = System.IO.File.ReadAllBytes(path);
                allImages.Add(cachedBytes);
    
            }
    
    
            return allImages;
        }
    
        public List<string> GetAllImagePath()
        {
            string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath + "/temp";
    
    
            Java.IO.File file = new Java.IO.File(path);
            Java.IO.File[] files = file.ListFiles();
            List<string> imagePaths = new List<string>();
            for (int i = 0; i < files.Length; i++)
            {
                imagePaths.Add(files[i].Path);
            }
            return imagePaths;
        }
    }
    

    } ```

    Then you can use it in the PCL.

    ``` List vs = DependencyService.Get().readImages();

            foreach (var item in vs)
            {
                myImage.Source = ImageSource.FromStream(() => new MemoryStream(item));
            }
    

    ```

    In the AndroidManifest.xml, do not add following permission and achieve the provider tag.

    ```

    <provider android:name="android.support.v4.content.FileProvider"
         android:authorities="${applicationId}.fileprovider"
         android:exported="false"
         android:grantUriPermissions="true">
    
      <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                       android:resource="@xml/file_paths"></meta-data>
    </provider>
    

    ```

    I read the first image to make a test.

    ``` private void ButtonClicked1(object sender, EventArgs e) { List vs = DependencyService.Get().readImages();

            //foreach (var item in vs)
            //{
            //    myImage.Source = ImageSource.FromStream(() => new MemoryStream(item));
            //}
            //Read the first image to make a test.
            myImage.Source = ImageSource.FromStream(() => new MemoryStream(vs[0]));
    
        }
    

    ```

    Here is running GIF.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, August 7, 2020 12:40 PM
  • User396123 posted

    Thank you kindly, your explanation and code was exactly what I needed

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Saturday, August 8, 2020 11:53 AM

All replies

  • User379860 posted

    You can write a dependenceService to achieve it.

    First of all create an interface in the PCL. public interface IFile { List<byte[]> readImages(); } Then achieve it in the android, Notice, I do not create a foler called MyFolder under Images folder, I have temp folder under Images folder, I use following code to read all of images to Byte[] ``` [assembly: Dependency(typeof(ReadImages))] namespace App15.Droid { public class ReadImages:IFile { public List readImages() { List imagePaths = GetAllImagePath();

            List<byte[]> allImages = new List<byte[]>();
            byte[] cachedBytes;
            foreach (var path in imagePaths)
            {
    
    
               cachedBytes = System.IO.File.ReadAllBytes(path);
                allImages.Add(cachedBytes);
    
            }
    
    
            return allImages;
        }
    
        public List<string> GetAllImagePath()
        {
            string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath + "/temp";
    
    
            Java.IO.File file = new Java.IO.File(path);
            Java.IO.File[] files = file.ListFiles();
            List<string> imagePaths = new List<string>();
            for (int i = 0; i < files.Length; i++)
            {
                imagePaths.Add(files[i].Path);
            }
            return imagePaths;
        }
    }
    

    } ```

    Then you can use it in the PCL.

    ``` List vs = DependencyService.Get().readImages();

            foreach (var item in vs)
            {
                myImage.Source = ImageSource.FromStream(() => new MemoryStream(item));
            }
    

    ```

    In the AndroidManifest.xml, do not add following permission and achieve the provider tag.

    ```

    <provider android:name="android.support.v4.content.FileProvider"
         android:authorities="${applicationId}.fileprovider"
         android:exported="false"
         android:grantUriPermissions="true">
    
      <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                       android:resource="@xml/file_paths"></meta-data>
    </provider>
    

    ```

    I read the first image to make a test.

    ``` private void ButtonClicked1(object sender, EventArgs e) { List vs = DependencyService.Get().readImages();

            //foreach (var item in vs)
            //{
            //    myImage.Source = ImageSource.FromStream(() => new MemoryStream(item));
            //}
            //Read the first image to make a test.
            myImage.Source = ImageSource.FromStream(() => new MemoryStream(vs[0]));
    
        }
    

    ```

    Here is running GIF.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, August 7, 2020 12:40 PM
  • User396123 posted

    Thank you kindly, your explanation and code was exactly what I needed

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Saturday, August 8, 2020 11:53 AM