Giter Site home page Giter Site logo

safmaui's Introduction

How to read/write from/to External Storage with .NET MAUI?

Definition:

Internal Storage: An Android App lives in a sandbox. Only the app can read and write data from/to this sandbox. External Storage: Is shared storage e.g. the documents or downloads folder and OTG, cloud and SD cards as well.

But how to access External Storage?

This works with the Storage Access Framework (SAF) Google introduced it with Android 4.4 (API level 19) Starting with API Level 24 Android 7 Nougat the SAF is the only way to access external memory.

Here you can find some infos about SAF

https://commonsware.com/Jetpack/pages/chap-content-001.html

https://developer.android.com/guide/topics/providers/document-provider

https://developer.android.com/training/data-storage/shared/documents-files

I was chaseing the Internet for a SAF wrapper for .Net Maui and found a package of

Mohammad Nikravan (madnik7) on: https://github.com/madnik7/PortableStorage

madnik7 has done a really good job. Unfortunatelly it is barely commented and documented.

In my example I concentrate on two things:

  • Copy a file from Internal Storage (e.g. FileSystem.Current.AppDataDirectory) to the external Storage (e.g /storage/emulated/0/Download).
  • Copy a file from External Storage to the Internal Storage.

You need to include the NuGet PortableStorage.Android package.

The SAF is URI based. To retrieve an URI the SAF provides a browser. With this browser the user selects a path in the External Storage. The browser returns an URI which can be stored for later usage. This needs to be done once by the user but can be repeated whenever the destination changes. There is no way (afaik) to generate a default URI by code to the e.g. downloads folder to omit usage of the browser. If anybody out there knows a way....

To call the methods in platform code from the shared project the successor of the Dependency Service is used. See both SafService.cs in the shared project and the platform project.

Here you can find some infos about that here:

https://blog.taranissoftware.com/platform-specific-code-using-partial-classes-in-net-maui

https://www.davidbritch.com/search?q=dependencyservice

A Maui built in way to access (only reading) external files is the FilePicker in Maui. No SAF is needed (only Permissions.StorageRead). See my example FilePickerTest

safmaui's People

Contributors

papnak avatar

Stargazers

Paramjit Singh avatar Soar avatar HuaFangYun avatar BlueSharkPartners avatar  avatar  avatar  avatar Charlie YAN avatar David Verrière avatar Lightbender255 avatar  avatar Roberto avatar

Watchers

Roberto avatar  avatar

Forkers

huafangyun

safmaui's Issues

cannot copy file larger than 130MB

when I use externalStorage.ReadAllBytes ,it reports like this:

java.lang.OutOfMemoryError: Failed to allocate a 134051088 byte allocation with 25165824 free bytes and 125MB until OOM, target footprint 296028280, growth limit 402653184

and I tried to limit the buffer size of stream, it works.

so maybe this function needs more tweak.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.