英文:
Android 11 Capture image using Camera
问题
以下是翻译好的内容:
文档 - https://developer.android.com/training/camera/photobasics
我已经按照所有必要的步骤使用相机进行图像捕获。
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
takePictureIntent.resolveActivity(getPackageManager())
- 这行代码始终返回 null。如果我跳过这个检查,相机就会打开,但应用程序会崩溃。
英文:
Documentation - https://developer.android.com/training/camera/photobasics
I have followed all the required steps to capture image using camera.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // its always null
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
takePictureIntent.resolveActivity(getPackageManager())
- this line always returns null. and if i skip this check than camera opens but app crashes.
答案1
得分: 20
Jaakko的答案是正确的,这里有一个简要的解释:
-
自API级别30以来,包的可见性发生了变化。https://developer.android.com/about/versions/11/privacy/package-visibility
-
为了使您的包管理器正常工作,您需要在您的AndroidManifest.xml中声明
<queries>
:
<manifest package="your.package.name">
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
</manifest>
这仅适用于默认的相机应用程序。如果您的应用程序正在使用某个第三方相机,您可以在这里找到一些信息:https://commonsware.com/blog/2020/08/16/action-image-capture-android-r.html
英文:
Jaakko's answer is correct, and here is a quick explanation:
-
Since API level 30, there have been changes in the package visibility. https://developer.android.com/about/versions/11/privacy/package-visibility
-
For your package manager to work properly, you need to declare
<queries>
in yourAndroidManifest.xml
:
Code:
<manifest package="your.package.name">
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
</manifest>
This works only for the default camera apps. If your app is using some 3rd party camera, you can find some info here: https://commonsware.com/blog/2020/08/16/action-image-capture-android-r.html
答案2
得分: 5
将以下内容添加到AndroidManifest.xml文件的manifest部分内:
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
英文:
Add these to AndroidManifest.xml inside the manifest section:
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论