问题描述
private fun clickPhoto(){
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
takePictureIntent.resolveActivity(requireActivity().packageManager)?.also {
val photoFile: File? = try {
createFile(requireActivity(),Environment.DIRECTORY_PICTURES,"jpg")
} catch (ex: IOException) {
Toast.makeText(requireActivity(),getString(R.string.create_file_Error,ex.message),Toast.LENGTH_SHORT).show()
null
}
photoFile?.also {
selectedPhotoPath = it.absolutePath
val photoURI: Uri = FileProvider.getUriForFile(
requireActivity(),BuildConfig.APPLICATION_ID + ".fileprovider",it
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI)
startActivityForResult(takePictureIntent,CAMERA_PHOTO_REQUEST)
}
}
}
}
这是我的点击照片函数,其中 resolveActivity() 发出警告 Consider adding a <queries> declaration to your manifest when calling this method; see https://g.co/dev/packagevisibility for details
。我知道从 API 30 开始,我们需要查询来访问其他已安装应用的信息。
我也知道这可以通过不使用 resolveActivity() 来解决,但我想学习如何向清单添加查询。
这是我的清单。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akaalistudios.employeemanagement">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.EmployeeManagement">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<Meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
</manifest>
解决方法
为了访问更广泛的已安装应用列表,应用可以指定有关他们需要直接查询和交互的应用的信息。这可以通过在 Android 清单中添加一个元素来完成。
对于大多数常见场景,包括任何以 startActivity() 开头的隐式意图,您无需更改任何内容!对于其他场景,例如直接从您的 UI 打开特定的第三方应用程序,开发人员必须像这样明确列出应用程序包名称或意图过滤器签名:
<manifest package="com.example.game">
<queries>
<!-- Specific apps you interact with,eg: -->
<package android:name="com.example.store" />
<package android:name="com.example.service" />
<!--
Specific intents you query for,eg: for a custom share UI
-->
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="image/jpeg" />
</intent>
</queries>
...
</manifest>
有关更多详细信息,您必须遵循以下链接: https://medium.com/androiddevelopers/package-visibility-in-android-11-cc857f221cd9 要么 Android 11 (R) return empty list when querying intent for ACTION_IMAGE_CAPTURE
我希望它会有所帮助。