标签: android-11

针对 Android api 版本 30 的应用程序的 Flutter 相机包权限

我最近将我的一个 flutter 应用程序的目标 sdk 升级到了 30。之后,拍照就不起作用了。它显示了此链接中提到的错误。

PlatformException(no_available_camera,没有可用于拍照的相机。,null)。

这与 android 11 中的包可见性更改有关。现在第一个链接中有一个解决方案,通过在清单文件中添加以下行来添加查询设备中安装的所有包的权限。

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
Run Code Online (Sandbox Code Playgroud)

这对我有用。但query_all_packages 的文档说明如下:

In the vast majority of cases, however, it's possible to fulfill your app's use cases by 
interacting with the set of apps that are visible automatically and by declaring the other 
apps that your app needs to access in your manifest file. To respect user privacy, your app 
should request the smallest amount of package visibility necessary in order for …
Run Code Online (Sandbox Code Playgroud)

android user-permissions flutter android-11

3
推荐指数
1
解决办法
2267
查看次数

权限拒绝:与 FileProvider Android 11 手机共享文件时出现安全异常

我在 Android 11 设备上遇到以下异常,但能够在电子邮件中共享这些文件,没有任何问题。

Java:Lang:SecurityException: Permission Denial:Reading androidx.core.content.FileProvider.uri content://com.example.myapp.fileprovider/external_files_files/images/default_image.jpg from pid= 10847, uid=1000 requires the provider be exported or granUriPermission.
Run Code Online (Sandbox Code Playgroud)

仅当我共享多个文件时,Android 11 设备才会出现问题,否则当我使用 Intent.setdata = uri(singal uri object) 时,单个文件可以正常工作,没有任何问题。

我的代码:

清单文件声明:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
        ...>
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.myapp.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
        ...
    </application>
</manifest>

Run Code Online (Sandbox Code Playgroud)

文件路径

<paths>
    <external-files-path
        name="images"
        path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)

共享代码。

    ArrayList<Uri> Urilist = new ArrayList<String>();
 
    // Adding multiple files as below.
    File imagePath = new File(Context.getExternalFilesDir(null), "images");
    File newFile = new File(imagePath, "default_image.jpg");
    Uri …
Run Code Online (Sandbox Code Playgroud)

android android-fileprovider android-securityexception android-11

3
推荐指数
1
解决办法
3674
查看次数

在 Android 11 中无法使用 taskAffinity 进行跨应用程序活动

我有两个应用程序,应用程序A和应用程序B,现在我想从应用程序A启动应用程序B中的一个活动,该活动的启动模式是\xe2\x80\x9csingleTask\xe2\x80\x9d。

\n

顺序:\n活动 X(应用程序 A)\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94> 活动 Y(应用程序 B)\xe2\x80\x94\xe2\x80\x94\xe2\ x80\x94> 活动 Z(应用程序 B,launchMode=\xe2\x80\x9csingleTask\xe2\x80\x9d)

\n

默认的 AndroidManifest 配置中,任务管理器中会显示两个应用程序,我希望用户在任务管理器中只能看到应用程序 A 标签,因此当他们在任务之间切换时,不要点击应用程序 B。在 Android 11 之前(Android R),我使用如下属性taskAffinity来解决这个问题。

\n

Activity X 和 Activity Z,将其添加到 AndroidManifet.xml 中

\n

android:taskAffinity="com.abc.xxx\xe2\x80\x9d

\n

因此,这些活动可以同时包含在一项活动任务中。在 Android 10 中,它可以工作。

\n

然而,它在 Android11 中不再适用\xe2\x80\x99。而且,我没有\xe2\x80\x99找到任何与这个场景相关的新功能。

\n

如何使 ApplicationA\xe2\x80\x99s 活动任务容纳 ApplicationB 的活动,其启动模式为 singleTask?让用户在任务管理器中只能看到一个任务(ApplicationA)。

\n

android cross-application taskaffinity android-activity android-11

3
推荐指数
1
解决办法
910
查看次数

如何将*多个*前台服务类型分配给一个Android服务

Android文档指出

android:前台服务类型

指定该服务是满足特定用例的前台服务。例如,“位置”的前台服务类型表示应用程序正在获取设备的当前位置,通常是为了继续用户发起的与设备位置相关的操作。

您可以将多个前台服务类型分配给特定服务。

问题

如何在 中指定多种类型AndroidManifest.xml

当前状态

到目前为止,我声明前台服务使用相机类型:

<application>
  <service
      android:name=".BackgroundService"
      android:exported="false"
      android:foregroundServiceType="camera"
      android:process=":camera_process" />
</application>
Run Code Online (Sandbox Code Playgroud)

但由于我必须对相机捕获的图像进行地理参考,因此我还必须声明foregroundServiceType="location".

我可以按照此处的要求轻松地在 Java 代码中引用这两种类型

与 R.attr.foregroundServiceType 属性中的摄像头对应的常量。使用相机设备或录制视频。对于 targetSdkVersion Build.VERSION_CODES.R 及更高版本的应用程序,如果未在清单和 Service.startForeground(int, android.app.Notification, int) 中指定此类型,则前台服务将无法访问摄像头。

通过使用这个标志:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    startForeground(NOTIFICATION_ID,  PlaceholderNotificationBuilder.build(this), FOREGROUND_SERVICE_TYPE_MANIFEST);
} else {
    startForeground(NOTIFICATION_ID, PlaceholderNotificationBuilder.build(this));
}
Run Code Online (Sandbox Code Playgroud)

但是如何foregroundServiceType在 中声明两个 s呢AndroidManifest.xml

android foreground-service android-11

3
推荐指数
1
解决办法
4436
查看次数

在 Android 11 中从最近的文件夹中选择 PDF 文件时会出现 NullPointException:Uri

下面显示了我的尝试方式,它给了我 NullPointException: Uri 从 Android 11 中的最近文件文件夹中选择 PDF 文件时。请让我知道如何从最近文件夹中获取 pdf 文件路径

            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
                if (Environment.isExternalStorageManager()) {
                    //todo when permission is granted
                    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("*/*");
                    String[] mimetypes = {"image/*", "application/*"};
                    intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
                    intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, path);
                    startActivityForResult(intent, REQ_PDF);
                } else {
                    //request for the permission
                    Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                }
            } else {
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("*/*");
                String[] mimetypes = {"image/*", "application/*"};
                intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes); …
Run Code Online (Sandbox Code Playgroud)

android android-11

3
推荐指数
1
解决办法
3174
查看次数

Android 11 更新破坏了我的文件写入代码

我之前使用过这个片段来编写一个带有名称和正文的简单文本文件:

public static File writeTextFile(String sFileName, String sBody) throws IOException {
        File root = new File(Environment.getExternalStorageDirectory(), FOLDER_NAME);

    if (!root.exists()) {
        logFile("Not yet created, creating right now: " + root.getPath());
        root.mkdirs();

        logFile("Creation was successful? " + (root.exists() ? "Yes." : "No."));

    } else {
        logFile("Path exists: " + root.getPath());
    }

  
    File       measurementFile = new File(root, sFileName);
    FileWriter writer  = new FileWriter(measurementFile);
    writer.append(sBody);
    writer.flush();
    writer.close();

    return measurementFile;

}
Run Code Online (Sandbox Code Playgroud)

除了 Android 11 之外,它运行良好,我今天在运行 Android 11 的 OnePlus N10 上进行测试后才意识到这一点。


问题是我没有收到任何描述的错误消息,只是一个简单的异常 …

android android-file android-11

3
推荐指数
1
解决办法
3143
查看次数

Android 11 语音搜索按钮不显示

在 中Android 11,当targetSdk设置为30并启用语音搜索时,麦克风图标不会显示在 上SearchView。但是,如果targetSdk设置为 29 ,它可以正常工作。它也适用于Android 10带有targetSdk 30.
有什么额外的事情需要做30吗?

菜单文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:title="Search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="always" />
</menu>
Run Code Online (Sandbox Code Playgroud)

清单文件

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />

            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

可搜索文件

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="Search"
    android:imeOptions="actionSearch"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
Run Code Online (Sandbox Code Playgroud)

主要活动

override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.search, menu)

        val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
        val …
Run Code Online (Sandbox Code Playgroud)

android searchview android-11

2
推荐指数
1
解决办法
394
查看次数

Android 11 中的后台麦克风访问引发异常

Microphone通过 Android 应用程序进行后台访问会Android 11在 Logcat 中引发以下异常:

W/ActivityManager: Foreground service started from background can not have location/camera/microphone access
Run Code Online (Sandbox Code Playgroud)

相同的代码在 Android 10 及更低版本中完美运行。如何修复它?

android microphone android-11

2
推荐指数
1
解决办法
5323
查看次数

Android 11 中不支持的 MIME 类型

在 Android 11 中遇到以下问题,并且在以下 11 中工作正常。录制音频文件时。

这是一个例外。

java.lang.IllegalArgumentException: Unsupported MIME type "audio/wav"
Run Code Online (Sandbox Code Playgroud)

代码

        File outFile = new File(outPath);
        long fileSize = outFile.length();

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, outPath);
        values.put(MediaStore.MediaColumns.TITLE, title.toString());
        values.put(MediaStore.MediaColumns.SIZE, fileSize);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");

        values.put(MediaStore.Audio.Media.ARTIST, getApplicationInfo().name);
        values.put(MediaStore.Audio.Media.DURATION, duration);

        values.put(MediaStore.Audio.Media.IS_MUSIC, true);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath);
        final Uri newUri = getContentResolver().insert(uri, values);
Run Code Online (Sandbox Code Playgroud)

android mime-types android-11

2
推荐指数
1
解决办法
7629
查看次数

在 Android 11 中创建 CSV 文件返回错误“java.io.FileNotFoundException:EPERM(不允许操作)”

您好,我正在尝试在我的 Android 应用程序中创建一个 csv 文件。当我尝试在下面的 android 10 上运行我的代码时,它可以工作。但我似乎找不到为什么我不能在已经是 Android 11 的设备上执行此操作的问题。

这是我的清单权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
        ...
        android:requestLegacyExternalStorage="true">
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我在下载文件夹中创建一个文件夹

@NonNull
public static File getStorageDirectory() {
    if (Environment.getExternalStorageState() == null) {

        File f = new File(Environment.getDataDirectory().getAbsolutePath() + "/Download/(Project Name)/");

        if(!f.exists())
            f.mkdirs();
        return f;
    } else {
        File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/(Project Name)/");
        if(!f.exists())
            f.mkdirs();
        return f;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我如何创建 csv …

csv android android-11

2
推荐指数
1
解决办法
8255
查看次数