How to read file form sd card Android 4.4 Xamarin - android

I am creating an app in which I would like to read a file from the sd card and then write to another file using info from the other file. When attempting to read the file I get the following error: System.UnauthorizedAccessException: Access to the path 'sdcard/android/data/App1.App1/files/' is denied.
Read Function:
private string ReadFile(string fileName)
{
var path = "sdcard/android/data/App1.App1/files/";
var filePath = Path.Combine(path.ToString(), fileName);
string text = File.ReadAllText(path.ToString());
return text;
}
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="App1.App1" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="16" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:label="Scout" android:icon="#drawable/Icon" android:theme="#style/CustomActionBarTheme"></application>
</manifest>

I have solved the issue and found that I was trying to read a folder instead of the file itself. When switching out the variable path with filePath int the read all text function the error went away.

Do not forget to add
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
into the manifest. This allows to read and write on SD card.
More details about uses-permission
It can also be a problem with your simulator and your sd card, read this topic

Related

Unable to read external storage in Xamarin.Forms for Android

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.

How to access flutter jni `/storage/emulated/0/Documents/` folder

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 get (OS Error: No such file or directory, errno = 2) in Flutter when file.delete(). At the same file.exist() return true

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?

Can not write to sdcard

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");

Is it possible to store all the application's data in sdcard?

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"/>

Categories

Resources