How to access DCIM Folder on Android

Anonymous
2021-12-22T14:05:15.043+00:00

am using Xamarin Community Toolkit 1.3.1 CameraView and can record video on Android.

Photo on Android the files are saved to '/data/user/0/com.CompanyName.AppName111/files/DCIM' which does not seem to be accessible, how to access it?

@Leon Lu (Shanghai Wicresoft Co,.Ltd.)

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,310 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,391 Reputation points Microsoft Vendor
    2021-12-23T05:51:27.35+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can create path by DependencyService for /data/user/0/com.CompanyName.AppName111/files/DCIM.

    1.Create an interface.

       public interface IGetImage  
           {  
               string GetImagePath();  
           }  
    
    1. Achieve it in android platform.DCIM folder do not existed. we need to create it. [assembly: Dependency(typeof(GetImagePathService))]
      namespace CommunityToolKitTest.Droid
      {
      class GetImagePathService : IGetImage
      {
      public string GetImagePath()
      {
                 var filePath = FileSystem.AppDataDirectory+ "/DCIM/";  
      
                 Java.IO.File file = new Java.IO.File(filePath);  
                 if (!file.Exists())  
                 {  
                     file.Mkdirs();  
                 }  
                 return filePath;  
             }  
         }  
      
      }

    Then save your image to this path.

       private async void cameraView_MediaCaptured(object sender, Xamarin.CommunityToolkit.UI.Views.MediaCapturedEventArgs e)  
               {  
                   var Imagedata=e.ImageData;  
                   var filePath= DependencyService.Get<IGetImage>().GetImagePath();  
                   var newFile = Path.Combine(filePath, "dd.png");  
    
                   using (Stream stream = new MemoryStream(Imagedata))  
                   using (var newStream = File.OpenWrite(newFile))  
                       await stream.CopyToAsync(newStream);  
               }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Lars Vinberg 101 Reputation points
    2021-12-22T21:33:47.417+00:00

    Look at Android.Provider.DocumentsContract

    0 comments No comments