安卓下的代码在MinGW下调试会从出错
.pro文件后面添加
contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
QT += androidextras
}
cpp文件中添加
#ifdef Q_OS_ANDROID
#include<QtAndroid>
#endif
读写权限
bool requestReadPermission() { #ifdef Q_OS_ANDROID QtAndroid::PermissionResult r = QtAndroid::checkPermission("android.permission.READ_EXTERNAL_STORAGE"); if(r == QtAndroid::PermissionResult::Denied) { // QMessageBox::information(0,"requestWritePermission22222","PermissionResult::Denied"); QtAndroid::requestPermissionsSync( QStringList() << "android.permission.READ_EXTERNAL_STORAGE" ); r = QtAndroid::checkPermission("android.permission.READ_EXTERNAL_STORAGE"); if(r == QtAndroid::PermissionResult::Denied) { return false; } } #endif return true; } bool requestWritePermission() { #ifdef Q_OS_ANDROID QtAndroid::PermissionResult r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE"); if(r == QtAndroid::PermissionResult::Denied) { // QMessageBox::information(0,"requestWritePermission","PermissionResult::Denied"); QtAndroid::requestPermissionsSync( QStringList() << "android.permission.WRITE_EXTERNAL_STORAGE" ); r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE"); if(r == QtAndroid::PermissionResult::Denied) { return false; } } return true; #endif }
我的读写函数
void ReadMyData() { requestReadPermission(); FILE *ffp; #ifdef Q_OS_ANDROID if((ffp=fopen((const char *)"/sdcard/s_v.quo","rb"))==NULL) #else if((ffp=fopen((const char *)"d:\s_v.quo","rb"))==NULL) #endif return ; fseek(ffp,0,SEEK_SET); fread(&m_save,sizeof(Def_Save),1,ffp); if(m_save.myunit<0||m_save.myunit>3) m_save.myunit=0; fclose(ffp); } void SaveMyData() { FILE *ffp; requestWritePermission(); #ifdef Q_OS_ANDROID if((ffp=fopen((const char *)"/sdcard/s_v.quo","wb+"))==NULL) #else if((ffp=fopen((const char *)"d:\s_v.quo","wb+"))==NULL) #endif { // fclose(ffp); return ; // exit(1); } fseek(ffp,0,SEEK_SET); fwrite(&m_save,sizeof(Def_Save),1,ffp); // qDebug()<<m_save.p_no; fclose(ffp); }