I am having difficulty with C++ Builder 11.1 Android trying to write a file to the Shared Documents folder. It worked with C++ Builder 10.3.1 but it doesn't work in 11.1
I have the permissions set properly in the Project Options Dialog box: Dangerous - Read External Storage = true Dangerous - Write External Storage = true
In the OnFormCreate method, I put in the necessary run time permissions as follows:
String FPermREAD;
String FPermWRITE;
DynamicArray<String> permissions;
FPermREAD = JStringToString(TJManifest_permission::JavaClass->READ_EXTERNAL_STORAGE);
FPermWRITE = JStringToString(TJManifest_permission::JavaClass->WRITE_EXTERNAL_STORAGE);
permissions.Length = 2;
permissions[0] = FPermREAD;
permissions[1] = FPermWRITE;
PermissionsService()->RequestPermissions(permissions, LocationPermissionRequestResult);
In the routine to write the file...
AnsiString SharedPath;
AnsiString FileName;
FILE *OutStream;
SharedPath = System::Ioutils::TPath::GetSharedDocumentsPath();
// SharedPath = "/storage/emulated/0/Documents"
FileName = SharedPath +"/"+MyFileName;
OutStream = fopen(FileName.c_str(),"wb"); // This fails in 11.1 but not in 10.3.1
if(OutStream) {
// write the file here
fclose(OutStream);
}
My Android device is Android 10. Any ideas why this is happening and how to fix it?
OnCreate
event in C++, use the constructor instead. And you should check withPermissionsService()->Is[Every]PermissionGranted()
before callingPermissionsService()->RequestPermissions()
. But more importantly, if you do callRequestPermissions()
, you need to check the result to make sure the permissions were actually granted before you perform the action you wanted permissions for. In the code provided, you are requesting write permissions to external storage, but there is nothing shown that indicates whether you were actually granted that permission.