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);
}
Related
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?
File oldFile = new File(dirPath+"/"+packageName+".apk");
Log.e("Path ",oldFile.getPath());
my log returns this:
E/Path:
/storage/emulated/0/Android/data/com.bazibaaz.app/files/com.YGD.GoldenBasket.apk
i have this file exactly on the path shown in the log, but this code:
oldFile.exists()
returns false!
i have permissions to read and write:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
here is the picture of file (to prove its existence):
( path: internal storage/storage/emulated/0/Android/data/com.bazibaaz.app/files/com.YGD.GoldenBasket.apk)
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
How to copy a file downloaded by download manager in internal storage's cache folder. I am using fileInputStream, but it through's permison denied. Is there any option to copy file
it has been a while since the question was asked but anyway, I found the answer this morning:
You simply can't copy files from you Caches folder. What you can do is to create your file in another folder (Files folder for example, which you get by doing getFilesDir()) and delete it if necessary !
Hope it helps,
Bye
Set permission in AndroidManifest.xml
<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.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
Here is the exact exception that's thrown:
java.io.FileNotFoundException: /mnt/sdcard/example.mp4 (Permission denied)
I literally copied and pasted example code from here. In addition, I also added this little bit of code to format my path correctly:
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
Here are the permissions included in my manifest:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.front"/>
Am I missing something completely obvious or is it something more?
You might want to check if the phone is in "Mass Storage Mode".
When USB cable is connected in this mode, you cannot access files on /sdcard.