'How to get the android <shared-storage> path from Qt?
I've had to upload a separate expansion file with my app (developed in Qt) in Google Play console. It's a simple rcc file. I have the name of file after the upload (it shows on the google console page). But I can't seem to find any info o how to get the shared-storage path (as mentioned) on the Android docs page (http://developer.android.com/google/play/expansion-files.html#StorageLocation).
I did come across this question (How to add android expansion files using Qt) where the user seems to have solved the issue but doesn't give any details on how to get the shared-storage path.
Solution 1:[1]
I've solved it:
I converted a qrc containing the large files to a binary using the qt rcc tool, then I used the following code to call the proper methods from the Qt Android Activity and retrieve the correct path to the expansion files:
QAndroidJniObject mediaDir = QAndroidJniObject::callStaticObjectMethod("android/os/Environment", "getExternalStorageDirectory", "()Ljava/io/File;");
QAndroidJniObject mediaPath = mediaDir.callObjectMethod( "getAbsolutePath", "()Ljava/lang/String;" );
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
QAndroidJniObject package = activity.callObjectMethod("getPackageName", "()Ljava/lang/String;");
QString dataAbsPath = mediaPath.toString()+"/Android/obb/"+package.toString()+"/yourExpansionFileName.obb";
QAndroidJniEnvironment env; // Don't know what this is for ?
if (env->ExceptionCheck()) { env->ExceptionClear(); } // Or this...?
bool loadResource = QResource::registerResource(dataAbsPath);
Don't forget to add #include <QAndroidJniEnvironment> in your app (and QT += androidextras in pro file), and last add this permission to the manifest (as the expansion file is located inside the external storage):
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
I didn't check if the file was there or not before loading (every device I tested worked straight), but the android docs say you might need to download it manually. There is also some catch to uploading expansion files, you can only add them with the second apk submission (in Google play).
Solution 2:[2]
This code may be help you.
QStringList systemEnvironment = QProcess::systemEnvironment();
looking for
- EXTERNAL_STORAGE=/storage/emulated/legacy
- EXTERNAL_ADD_STORAGE=/storage/external_SD
- SECONDARY_STORAGE=/storage/external_SD
- ...
Solution 3:[3]
This code worked for me:
QString s = QStandardPaths::writableLocation(QStandardPaths::movieLoaction);
QDir MyDir;
s = s + "/MyFolder"
MyDir.mkdir(s);
In your Android device you must check the storage permissions for your application. If not add this permission then run the application.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Paul Roub |
| Solution 3 | the Tin Man |
