Fail to create a folder in android app with NDK - android

While running the app on the emulator, system reports an error while cpp code trying to create a directory in folder "/home/cocadas/Workspace/android-project/JNIAppSample".
Java will call a JNI cpp function to create the directory.
The cpp source code is as following:
static int createEventDir(void)
{
int result;
int stringLen;
time_t currentTime = time(0);
struct tm * now = localtime(&currentTime);
stringLen = sprintf(thisEventParms.eventDirectory,
ADAN_EVENT_BASE_DIR, now->tm_mon + 1,
now->tm_mday, now->tm_hour,
now->tm_min, now->tm_sec);
if (stringLen > 0)
{
result = mkdir(thisEventParms.eventDirectory, 0700);
}
else
{
// TBD: Error, unable to make event directory
result = -1;
}
return(result);
}
Android Studio debug reports ​result = -1 after executing result = mkdir(thisEventParms.eventDirectory, 0700);​ Also, debug reports thisEventParms.eventDirectory = "/home/cocadas/Workspace/android-project/JNIAppSample", which is expected.
After some research, I add one permission in manifest like the following:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
But it still gives result = -1
Any comment or suggestion?

I guess that the android emulator has the same structure as any android device.
So, /home/cocadas...etc doesn't exist in your emulator.
If the response is -1, usually seems that your path is not writable.
In conclusion, writable path could be "/sdcard/your_directory." and of course you will need to add the permission you written above.
If you want to get an writable path but inside of your apk use this code.
String config_path=m_context.getApplicationContext().getFilesDir().toString();
Cheers.

Related

How can I access a directory outwith my programs with Flutter?

I am trying to list the files of a directory on Android through flutter, I am using file_picker to access it but it lists nothing even for a directory full of files.
I have tried opening a directory with files in it and printing it out:
void _pickDirectory() async {
final path = await FilePicker.platform.getDirectoryPath();
if (path == null) {
print('Canceled');
} else {
final directory = Directory(path);
final files = directory.listSync();
print(files);
}
}
but it just prints out an empty array.
I've added
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
to my android/app/src/main/AndroidManifest.xml and reinstalled the app but it still shows no files.
Any help is appreciated, thank you

C++ Builder 11.1 Android can't write a file to the Shared Documents folder

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?
Add requestLegacyExternalStorage="true" to application tag in manifest file.

How to read Android 10 devices files with Flutter App

Am using this code to try to read a particular directory an android devices.. this is the code in a released application. it works fine on all devices except SamSung devices..
fetchFromOriginalDirectory() async{
var result = await PhotoManager.requestPermission();//getting permission to check files
if (result) {
Directory dir = Directory('/storage/emulated/0/');
List<FileSystemEntity> _files;
_files = dir.listSync(recursive: true, followLinks: false).reversed.toList();//getting all files(stickers) in this directory
for(FileSystemEntity entity in _files) {//for each file gotten do this
String path = entity.path;
I first thought the problem was this line "/storage/emulated/0/" but after using multiple plugins to check for the right path. i found out the paths are the same. But for some reason the app cant read the files on samsung files. even after all permissions have been granted
Hey guys i fixed it by doing this
<application
android:requestLegacyExternalStorage="true" //adding this line
>
In the android manifest file.

Qt C++ library in Android Eclipse project: QSQLITE driver not loaded

I've created a Qt dynamic lib that uses Qt SQL to open an SQLite database, but I'm getting this error:
QSqlDatabase: QSQLITE driver not loaded
QSqlDatabase: available drivers:
The DLL was working fine as part of a Qt Android application, however I need to use it through JNI from an existing Java application developed in Eclipse.
This is the shortest example code that reproduces the problem. I load the library from Java and call its init() method:
System.loadLibrary("plugins_sqldrivers_libqsqlite");
System.loadLibrary("Qt5Sql");
System.loadLibrary("MyQtLib");
MyQtLib.init();
And inside the Qt library I just call QSqlDatabase::addDatabase():
JNIEXPORT void JNICALL Java_test_MyQtLib_foo(JNIEnv *, jclass)
{
// Manually create a QCoreApplication instance.
int argc = 1;
static char arg[] = "";
static char *arg2 = arg;
app = new QCoreApplication(argc, &arg2);
// Try to add an SQLite db connection.
QSqlDatabase::addDatabase("QSQLITE");
}
Since the error is QSQLITE driver not loaded, and the Qt library was working inside a Qt application, I assume that Qt is doing some initialization that I'm missing.
But this didn't remove the error, so it must be something else. Normally, the Qt application will use QtApplication.java and QtActivity.java to perform some initialization, so they must be doing something more there that I'm not doing.
Eventually I stepped into the Qt sources to see how the SQLITE plugin is loaded (for the desktop build at least).
The relevant function was QFactoryLoader::update(). In it I noticed that it iterates all the directories in QCoreApplication::libraryPaths():
QStringList paths = QCoreApplication::libraryPaths();
for (int i = 0; i < paths.count(); ++i) {
If any of them has a sub-directory named "sqldrivers", it goes inside it and tries to load all the dynamic libraries in that sub-directory.
I then printed out the library paths in a test project I ran directly from Qt Creator - qDebug() << a.libraryPaths();, and I saw this path - /data/data/org.qtproject.example.untitled/qt-reserved-files/plugins. In this directory on my android phone there was a subdirectory named sqldrivers, that contained a single file - libqsqlite.so.
I then checked the .java files, and indeed QtActivity::startApp() adds the library path:
boolean bundlingQtLibs = false;
if (m_activityInfo.metaData.containsKey("android.app.bundle_local_qt_libs")
&& m_activityInfo.metaData.getInt("android.app.bundle_local_qt_libs") == 1) {
localPrefix = getApplicationInfo().dataDir + "/";
pluginsPrefix = localPrefix + "qt-reserved-files/";
cleanOldCacheIfNecessary(localPrefix, pluginsPrefix);
extractBundledPluginsAndImports(pluginsPrefix);
bundlingQtLibs = true;
}
The solution then would be to ensure that there is a sqldrivers/libqsqlite.so somewhere on the phone, and then add the parent folder of sqldrivers to the library path using QCoreApplication::addLibraryPath().

Android - writing/saving files from native code only

I'm trying to build an Android app which makes use of the NativeActivity facility of the NDK.
I'm having the following structure:
a bunch of native shared libraries installed in /system/vendor/<company>; I'm working
with a custom built Android image so there's no problem having the libraries there with
proper permissions and everything
a couple of applications using the NativeActivity that depend in turn on the libraries
mentioned above
The libraries installed in the /system/vendor and my applications use a couple of
configuration files. There's no problem reading them using the standard C API
fopen/fclose. But those libraries and my application also need to store some files
as the result of their operation, like configuration, some run-time parameters, calibration
data, log files etc. With the storing of the files there is a slight issue as I'm not allowed to write into /system/vendor/... (as the file system under "/system/..." is mounted read-only and I do not want to hack on that).
So what would be the best way to create and store those files and where would be the
best "conforming with Android" storage area ?
I've been reading a couple of threads in the android-ndk Google group and here on SO that mention either the internal application private storage or the external SD card, but as I do not have extended experience with Android I'm not sure what would be the proper approach. If the approach involves some specific Android API a small code example in C++ would be very helpful; I've seen a couple of examples involving Java and JNI (e.g. in this SO question) but I would like to stay away from that right now.
Also there seems to be a problem with using from C++ the native activity's
internalDataPath/externalDataPath pair (a bug that makes them be always NULL).
For relatively small files(application config files, parameter files, log files etc.)
is best to use the internal application private storage, that is /data/data/<package>/files.
The external storage if it exists at all (being it SD card or not) should be used for large files that do not need frequent access or updates.
For the external data storage the native application has to "request" the correct permissions in the application's AndroidManifest.xml:
<manifest>
...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
</uses-permission>
</manifest>
For the internal application private storage fopen/fclose(or C++ stream equivalents if available) API could be used. Following example illustrates using the Android NDK AssetManager to retrieve and read a configuration file. The file must be placed into the assets directory inside the native application’s project folder so that the NDK build could pack them inside the APK. The internalDataPath/externalDataPath bug I was mentioning in the question was fixed for the NDK r8 version.
...
void android_main(struct android_app* state)
{
// Make sure glue isn't stripped
app_dummy();
ANativeActivity* nativeActivity = state->activity;
const char* internalPath = nativeActivity->internalDataPath;
std::string dataPath(internalPath);
// internalDataPath points directly to the files/ directory
std::string configFile = dataPath + "/app_config.xml";
// sometimes if this is the first time we run the app
// then we need to create the internal storage "files" directory
struct stat sb;
int32_t res = stat(dataPath.c_str(), &sb);
if (0 == res && sb.st_mode & S_IFDIR)
{
LOGD("'files/' dir already in app's internal data storage.");
}
else if (ENOENT == errno)
{
res = mkdir(dataPath.c_str(), 0770);
}
if (0 == res)
{
// test to see if the config file is already present
res = stat(configFile.c_str(), &sb);
if (0 == res && sb.st_mode & S_IFREG)
{
LOGI("Application config file already present");
}
else
{
LOGI("Application config file does not exist. Creating it ...");
// read our application config file from the assets inside the apk
// save the config file contents in the application's internal storage
LOGD("Reading config file using the asset manager.\n");
AAssetManager* assetManager = nativeActivity->assetManager;
AAsset* configFileAsset = AAssetManager_open(assetManager, "app_config.xml", AASSET_MODE_BUFFER);
const void* configData = AAsset_getBuffer(configFileAsset);
const off_t configLen = AAsset_getLength(configFileAsset);
FILE* appConfigFile = std::fopen(configFile.c_str(), "w+");
if (NULL == appConfigFile)
{
LOGE("Could not create app configuration file.\n");
}
else
{
LOGI("App config file created successfully. Writing config data ...\n");
res = std::fwrite(configData, sizeof(char), configLen, appConfigFile);
if (configLen != res)
{
LOGE("Error generating app configuration file.\n");
}
}
std::fclose(appConfigFile);
AAsset_close(configFileAsset);
}
}
}

Categories

Resources