How to convert memory stream to FileResult

Chinmay Dole 245 Reputation points
2024-07-22T11:35:58.1533333+00:00

I had asked to compress the image file in the following link:

https://nam.safelink.emails.azure.net/redirect/?destination=https%3A%2F%2Flearn.microsoft.com%2Fen-us%2Fanswers%2Fc%2F1646218&p=bT1iMDAyYjk0OC1hMTU3LTRhOGUtODA0MS05ODM4NTQ0N2Y2ZjEmdT1hZW8mbD0xNjQ2MjE4

I don't want to show the image. Instead, I need to save the image locally using the file result. How is this possible?

Developer technologies .NET .NET MAUI
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-07-24T03:00:11.4033333+00:00

    Hello,

    In MAUI, you can use MediaPicker to select or take pictures and save them locally. You could refer to the necessary code and comments below.

    Step 1. Add the following necessary permissions in the AndroidManifest.xml file of MAUI.

    
    <uses-permission android:name="android.permission.CAMERA" />
    
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
    
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
    
    <!-- Required only if your app needs to access images or photos that other apps created -->
    
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    
    <!-- Required only if your app needs to access videos that other apps created -->
    
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
    
    <!-- Required only if your app needs to access audio files that other apps created -->
    
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
    
    <queries>
    
    	<intent>
    
    		<action android:name="android.media.action.IMAGE_CAPTURE" />
    
    	</intent>
    
    </queries>
    
    

    Step 2. Call MediaPicker to save the file locally.

    
    public async Task PickPhotoAndSaveAsync()
    
    {
    
        // If you need to save the captured file, you need to call the MediaPicker.Default.CapturePhotoAsync() method here
    
        FileResult photo = await MediaPicker.Default.PickPhotoAsync();
    
     
    
        if (photo != null)
    
        {
    
            // save the file into local storage
    
            string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
    
     
    
            using Stream sourceStream = await photo.OpenReadAsync();
    
            using FileStream localFileStream = File.OpenWrite(localFilePath);
    
     
    
            await sourceStream.CopyToAsync(localFileStream);
    
        }
    
    }
    
    

    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.


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.