Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

c++ - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...