Android 11 使用相机拍摄照片

huangapple go评论80阅读模式
英文:

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:

Code:

&lt;manifest package=&quot;your.package.name&quot;&gt;
&lt;queries&gt;
    &lt;intent&gt;
        &lt;action android:name=&quot;android.media.action.IMAGE_CAPTURE&quot; /&gt;
    &lt;/intent&gt;
&lt;/queries&gt;
&lt;/manifest&gt;

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:

&lt;queries&gt;
  &lt;intent&gt;
    &lt;action android:name=&quot;android.media.action.IMAGE_CAPTURE&quot; /&gt;
  &lt;/intent&gt;
&lt;/queries&gt;

huangapple
  • 本文由 发表于 2020年9月18日 14:49:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63950633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定