Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create files on Android with Xamarin

I have a Xamarin-Studio App for Android and I simply want to download files and save them locally. But when I try to create a file in the files folder I get an exception:

File.Create("data/data/com.company.app/files/newFile.png");

gives me:

System.UnauthorizedAccessException
Access to the path 'data/data/com.company.app/files/newFile.png' is denied.

What am I doing wrong?

like image 798
codingFriend1 Avatar asked Feb 11 '14 16:02

codingFriend1


People also ask

Is Xamarin being discontinued?

Xamarin support will end on May 1, 2024 for all Xamarin SDKs. Android 13 and Xcode 14 SDKs (iOS and iPadOS 16, macOS 13) will be the final versions Xamarin will target.

Is Xamarin deprecated Android?

Not dead but possibly moribund. In May 2020, Microsoft announced that Xamarin. Forms, a major component of its mobile app development framework, would be deprecated in November 2021 in favour of a new . Net based product called MAUI - Multiform App User Interface.

Is Xamarin good for Android development?

Ultimately, appreneurs should use Xamarin for building mobile apps, as the platform offers an excellent means to build mobile apps without utilizing OS-specific languages like Swift or Java. The advantage of this is more efficient and comprehensive development as codebases can be shared cross-platform.

Does Xamarin work with iOS and Android?

Xamarin. Forms is an open source mobile UI framework from Microsoft for building iOS, Android, & Windows apps with . NET from a single shared codebase.


2 Answers

You should use Environment or IsolatedStorage. For example:

var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filename = Path.Combine(path, "newFile.png");
like image 78
SKall Avatar answered Oct 22 '22 00:10

SKall


I am coding Xamarin with VS2013. I had the access denied error for a directory created with the application I am writing. My application creates a directory called /storage/emulated/0/POIApp by concatenating via:

System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "POIApp");

I found that I had to use VS2013 to edit the "properties" of my application (POIApp), i.e., right-click the project icon in the solution explorer; choose properties from the pop-up menu. A new tab appears in the main VS2013 window. On the left there are a few choices, e.g., Application, Android Manifest, Android Options, Build, etc. Choose "Android Manifest". At the bottom of the main panel is a section "required permissions". My problem was solved when I checked "READ_EXTERNAL_STORAGE" and "WRITE_EXTERNAL_STORAGE".

like image 39
user3784130 Avatar answered Oct 22 '22 01:10

user3784130