在Android中使用相机活动

slr*_*ert 35 java camera android android-activity

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

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
        this.startActivityForResult(camera, PICTURE_RESULT);
Run Code Online (Sandbox Code Playgroud)

你想从你展示的漂亮相机中取回图像 - 但是如何?

slr*_*ert 23

如果你想让图像恢复光彩,请将uri传递给EXTRA_OUTPUT额外的Intent.如果你对一个小的位图很好(你应该这样),只需正常调用意图.

现在您有两个选项,处理EXTRA_OUTPUT额外返回的图像的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) {...}
    }
Run Code Online (Sandbox Code Playgroud)

你去吧!

  • 谢谢slrobert这对我帮助很大.许多其他教程解释了从头开始实现摄像头功能,而不是简单地提出一个`ActivityForResult`并让默认的摄像头活动处理事情! (2认同)