Java 在 Android 中使用相机活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2314958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:02:14  来源:igfitidea点击:

Using the camera activity in Android

javaandroidcameraandroid-activity

提问by slrobert

If you want to use the built-in camera activity which uses the native Android camera, simply do the following.

如果您想使用使用原生 Android 相机的内置相机活动,只需执行以下操作。

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
        this.startActivityForResult(camera, PICTURE_RESULT);

You want to get the images back from the nifty camera you displayed -- but how?

您想从您展示的漂亮相机中取回图像——但是如何呢?

回答by slrobert

If you want to get the image back in its full glory, pass in a uri to the Intent within the EXTRA_OUTPUT extra. If you're fine with a smallish bitmap (and you should be), just call the intent as normal.

如果您想让图像恢复原状,请在 EXTRA_OUTPUT 额外内容中将 uri 传递给 Intent。如果您对较小的位图感到满意(您应该如此),只需像往常一样调用意图。

Now you have two options, deal with the uri of the image that is returned in the EXTRA_OUTPUT extra, or do the following in your onActivityResult method:

现在您有两个选择,处理在 EXTRA_OUTPUT extra 中返回的图像的 uri,或者在您的 onActivityResult 方法中执行以下操作:

if (requestCode == PICTURE_RESULT) //
             if (resultCode == Activity.RESULT_OK) {
                // Display image received on the view
                 Bundle b = data.getExtras(); // Kept as a Bundle to check for other things in my actual code
                 Bitmap pic = (Bitmap) b.get("data");

                 if (pic != null) { // Display your image in an ImageView in your layout (if you want to test it)
                     pictureHolder = (ImageView) this.findViewById(R.id.IMAGE);
                     pictureHolder.setImageBitmap(pic);
                     pictureHolder.invalidate();
                 }
             }
             else if (resultCode == Activity.RESULT_CANCELED) {...}
    }

And there you go!

你去吧!