xamarin forms copy photos to a directory accessible by mail program

WillAutioItrax 206 Reputation points
2022-03-24T04:13:01.94+00:00

We can successfully take photos in our Xamarin Forms cross platform app. A new task is to copy the photo to a location on the device that can be accessed by the local email program.
How can I get the proper folder for iOS, Android and UWP? Or do I need a different command for each?
Thanks!

Developer technologies | .NET | Xamarin
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2022-03-25T09:23:19.423+00:00

    Hello,

    You need to use DependencyService to implement the function in different platforms. You can refer to DependencyService to get more details.

    You can refer to the following content:

    In Android:

    You can try the following code to save photos into public directory:

       private void CreateFileTest(string internalPath, string name)  
               {  
       		// decode a bitmap from a local file path.  
                   Bitmap bitmap = BitmapFactory.DecodeFile(internalPath);  
                   System.IO.Stream fos;  
                   if (Build.VERSION.SdkInt >= BuildVersionCodes.Q)  
                   {  
                       ContentValues contentValues = new ContentValues();  
                       contentValues.Put(MediaStore.IMediaColumns.DisplayName, name + ".jpg");  
                       contentValues.Put(MediaStore.IMediaColumns.MimeType, "image/jpg");  
                       contentValues.Put(MediaStore.IMediaColumns.RelativePath, Android.OS.Environment.DirectoryPictures);  
                       var imageUri = Android.App.Application.Context.ContentResolver.Insert(MediaStore.Images.Media.ExternalContentUri, contentValues);  
                       fos = Android.App.Application.Context.ContentResolver.OpenOutputStream(imageUri);  
                       bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, fos);  
                   }  
                   else  
                   {  
                       String imagesDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).ToString();  
                       var path = System.IO.Path.Combine(imagesDir, name , ".jpg");  
                       fos = new FileStream(path,FileMode.Create);  
                       bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, fos);  
                   }  
                     
               }  
    

    You can refer to Storage updates in Android 11 to know changes.

    In IOS:

    You can refer to Sharing with the Files app to know how to share files with the Files App.

    In UWP:

    You can use the following code to copy files:

       private void CopyFileTest()  
       {  
           var sourcepath = "your photo path";  
           var targetpath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments));  
           // the 'true' means that will overwrite duplicate file.  
           System.IO.File.Copy(sourcepath, targetpath, true);  
       }  
    

    Then, the file will be copied to C:\Users\username\Documents\test.txt.

    You can refer to Environment.SpecialFolder Enum to get more details.

    Best Regards,

    Alec Liu.


    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.