Qt for Android 10 适配外部存储权限 - 小众知识

Qt for Android 10 适配外部存储权限

2021-09-22 04:50:46 苏内容
  标签: Qt/Android
阅读:7313

背景

  之前我们的项目一直是运行在Android5.1和Android7.1.2的系统下,后面有新的需求呀要运行在Android 10下面,当时不知道有多少坑,以为只需要简单的适配即可。但由于我们的应用有一些特殊的需求(后面会提到),因此适配过程中还是有不少坑。

问题

  从Android Q(即 Android 10)开始,应用访问外部存储的私有目录(即Context.getExternalFilesDir())不需要申请READ_EXTERNAL_STORAGE 或 WRITE_EXTERNAL_STORAGE权限,该空间内的数据为应用独有。而且就算应用申请了,也只能访问外部存储的私有目录(即APP包内部目录),若是访问了除了私有目录之外的其他外部储存(如/sdcard/目录),会抛出java.io.FileNotFoundException异常。

解决办法

一、 在AndroidManifest.xml的Application节点设置android:requestLegacyExternalStorage="true"。

source.android.google.cn/devices/sto…

  官方描述:从 Android 10 开始,以 Android 9 及更低版本为目标平台的应用默认使用旧版存储,并且可以选择使用隔离存储。以 Android 10 为目标平台并默认使用隔离存储的应用可以暂时选择停用隔离存储。使用控制存储模型的清单属性 requestLegacyExternalStorage 可更改默认状态。

即Android提供了requestLegacyExternalStorage机制,来帮助应用过渡,使用原来的机制继续读写存储卡(注意这只是个过渡属性,不能保证在未来的哪个版本会被移除)。

二、如果是媒体集合(照片、视频、音频),使用MediaStore。详情查阅:developer.android.google.cn/training/da…

I have been using this piece of code to share stuff on my Qt-Android app. It works perfectly to share videos and images on any version of Android lower than 10. My problem specifically, it's that the social media panel (for sharing) is not showing up when I called it, for Android 10. In fact, I don't get any error message in the console.

    String target = "/real/path/to/file/mp4";
    Uri fileUri;
    File mediaFile = new File(target);    if (mediaFile.exists())
        fileUri = Uri.fromFile(mediaFile);
    String mimeType = "video/mp4";

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
    sendIntent.setType(mimeType); 
    if (sendIntent.resolveActivity(QtNative.activity().getPackageManager()) != null) {        if (DEBUG) {
            String msg = "Creating social media chooser...";
            Log.i(TAG, msg);            appendLog(msg);
        }
        QtNative.activity().startActivity(Intent.createChooser(sendIntent, title));
    } else {        if (DEBUG) {
            String msg = "Fatal Error: Intent not resolved!";
            Log.e(TAG, msg);            appendLog(msg);
        }        return false;
    }

The specific line where the social media should pop up is this:

QtNative.activity().startActivity(Intent.createChooser(sendIntent, title));

Any suggestion/workaround to fix this issue in Android 10?

PS: I already added this parameter android:requestLegacyExternalStorage="true" into the AndroidManifest.xml config file with no results.



Android enforces restricted file access starting with Android Q which can be turned off (android:requestLegacyExternalStorage="true" in the AndroidManifest.xml) but will be mandatory in Android R in 2020. Qt should somehow provide scoped file access. At the moment we can get scoped file access as follows:

  1. Request file access in a Java method.
  2. Pass the file handle from Java to C++ and use it as a parameter in QFile().


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1