In the Xamarin.Forms application we are developing, called "myApp", targeting Android devices only, we need to be able to read from (and possibly write to) the text file
storage/emulated/0/Android/data/com.myApp.configuration/myAppStuff.txt
which in the Windows 10 development platform appears as
This PC\DEVICE TYPE\Internal shared storage\Android\data\com.myApp.configuration\
In the AndroidManifest.xml file for the project, we have included
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" ... >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"/>
</manifest>
This is confirmed in Visual Studio 2022 by navigating to
Project > myApp.Android Properties > Android Manifest > Required permissions:
and observing the required permissions are asserted.
When the application is first started in the debug mode, in MainActivity.cs
AndroidX.Core.App.ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ReadExternalStorage)
returns "false", so we invoke
AndroidX.Core.App.ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ReadExternalStorage, Android.Manifest.Permission.WriteExternalStorage, Android.Manifest.Permission.ManageExternalStorage }, 0)
Android shows a (non-modal) dialog
for which we select "ALLOW".
Later, the code
File.Exists("/storage/emulated/0/Android/data/com.myApp.configuration/myAppStuff.txt")
returns "true"
but the code
mystreamreader = new StreamReader("/storage/emulated/0/Android/data/com.myApp.configuration/myAppStuff.txt");
throws the exception with Message value of
"Access to the path \"/storage/emulated/0/Android/data/com.myApp.configuration/myAppStuff.txt\" is denied."
A later check shows that the code
AndroidX.Core.App.ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ManageExternalStorage)
returns the value "false".
Why?
I can't reproduce your problem. But you said:
we need to be able to read from (and possibly write to) the text file
So I created a sample to read and write the text file:
In my AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0"
package="com.companyname.app21">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
<application android:label="App21.Android" android:theme="#style/MainTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Please make sure the package's value in your AndroidManifest.xml file is com.myApp.configuration.
And the code about create, read and write the file:
FileStream filestream = new FileStream("/storage/emulated/0/Android/data/com.companyname.app21/test.txt",
FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.ReadWrite);
// create the text file
File.WriteAllText("/storage/emulated/0/Android/data/com.companyname.app21/test.txt", "this is content");
// write the text file
var content = File.ReadAllText("/storage/emulated/0/Android/data/com.companyname.app21/test.txt");
//read the text file
Which Android version? As of Android13, EXTERNAL_STORAGE is basically gone. CheckSelfPermission for it will auto-answer false, just as if you answered "No and do not ask again" before. They want you to move to different APIs, e.g. ask a Document Picker to get arbitrary files or be a Document Provider to provide them.
You need to read data from a file that exists in the path /storage/emulated/0/Documents/. There is no problem in checking the existence of a file. But I access this path with 'jni' i.e. 'C' code and read the data from the file.
Failed to open file due to permission problem.
Is there a way to access files in /storage/emulated/0/Documents/ in jni C?
permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
//in <application...
android:requestLegacyExternalStorage="true"
android:preserveLegacyExternalStorage="true"
code
int nResult = (i ~/ 100) * 100;
File agingFile = File("/storage/emulated/0/Documents/$nResult/$i.C03");
await platform.invokeMethod("readFile", agingFile.path);
///JAVA
if(call.method.equals("readFile")){
String path = call.arguments();
ReadFile(path);
}
I am developing a Flutter app. I am trying to add a functionality to delete a file in device storage. When i initialize a file object with the path to the file, and run file.exist() i get true. But when i run file.delete() i get
(OS Error: No such file or directory, errno = 2)when file.delete(). At the same file.exist() return true!
I have these permissions in my manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
<uses-permission android:name="android.permission.STORAGE_INTERNAL" />
and
<application
android:requestLegacyExternalStorage="true"
And i am using permission_handler to ask for permission first.
Any idea?
I'm trying to create folder on the SDCard on Android 2.3 device:
final File downloadFolder = new File(FILES_PATH);
if (!downloadFolder.exists()) {
Log.i(TAG, "Creating tmp directory: " + downloadFolder.mkdirs());
}
And mkdirs() returns false. FILES_PATH is the same that getExternalStorage() returns - /mnt/sdcard/.tmp/
SD card is writable from the cli with root.
Permission:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.venturezlab.tvupdater"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Why?
Try mkdir() instead of mkdirs()
Try this
final File downloadFolder = new File(Environment.getExternalStorageDirectory()+"/.tmp");
We need to create our own file structure on sdcard for storing preference files,database files and files ?
Is this possible ? I know we can store files anywhere on sdcard but not sure about SQLiteDatabase files and Shared Preference files .
Please provide me suggestions on this.
Thanks in Advance.
Ya its possible while developing an application.in manifest just give
android:installLocation="preferExternal"
then the application and the file of the application will be dierectly written to the external storage of the device that is to the SD card.You will be having two more options "auto" and "intenalOnly" just like this
<manifest xmlns:android="*******************"
package="*************"
android:versionCode="1"
android:installLocation="auto"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>