首次安装应用无法解析android 11中的com.android.camera.action.CROP

问题描述

我正在使用Android 11上的字符串“ com.android.camera.action.CROP”开始隐式意图裁剪。 首次安装应用程序时无法通过此代码解决其活动。

   Intent intent = new Intent("com.android.camera.action.CROP");


    intent.setType("image/*");

    //to check whether there is an cropping app present or not
    List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(
            intent,MATCH_DEFAULT_ONLY);

它不是第一次解决,而是第二次运行,它获得了可以处理意图的活动。

解决方法

我假设您以Android 11(API 30)为目标,这要求您在AndroidManifest.xml内的queries节点中为外部应用指定所有意图,如下所示:

<queries>
    ...
    <intent>
        <action android:name="com.android.camera.action.CROP" />
    </intent>
    ...
</queries>

详细了解herehere。还有关于此事here的中篇文章。

注意: 意图com.android.camera.action.CROP基于AOSP的相机应用程序,在某些设备上可能会丢失,请参考Commonsware的旧博客文章:https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html

,

就我而言,我在AndroidManifest.xml中使用以下<queries>配置,并且可以正常工作。

<manifest>
  // ...
  <queries>
      <intent>
          <action android:name="com.android.camera.action.CROP" />
          <data android:scheme="content"
            android:mimeType="image/*"/>
      </intent>
  </queries>
</manifest>

resolveActivity

    val intent = Intent("com.android.camera.action.CROP").apply {
        type = "image/*"
        data = photoUri
    }
    if (intent.resolveActivity(context.packageManager) != null) {
        // grant uri permission here
    }

经过测试,我发现关键是queries中的 android:mimeType 必须与Intent中设置的type对齐。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...