使用意图拍摄图像后,相机不会关闭

时间:2018-02-02 16:59:19

标签: android camera

我想创建一个简单的应用程序,打开相机,拍照并在用户接受此照片后,关闭相机并在图像视图中显示。 请注意,目前我不想保存此图片,只显示它。 我使用了android文档中的代码来做这个,但是在拍摄照片后相机没有关闭,因此我无法在图像视图中看到图片。 代码来自android文档,所以它真的很令人沮丧,它不起作用。

Anyonw知道为什么吗?

提前致谢!

我的Java代码是:

 @Override
protected void onStart(){
    super.onStart();
    //here is the camera request
    dispatchTakePictureIntent();
}


static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        ImageView mImageView = (ImageView) findViewById(R.id.fullscreen_content);
        mImageView.setImageBitmap(imageBitmap);
    }
}

我的布局是这样的(大部分是由全屏幕活动准备的,所以唯一重要的是ImageView):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="com.gj_games.cameratest.CameraInfo">

<!-- The primary full-screen view. This can be replaced with whatever view
     is needed to present your content, e.g. VideoView, SurfaceView,
     TextureView, etc. -->
<ImageView
    android:id="@+id/fullscreen_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<!-- This FrameLayout insets its children based on system windows using
     android:fitsSystemWindows. -->
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:id="@+id/fullscreen_content_controls"
        style="?metaButtonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/black_overlay"
        android:orientation="horizontal"
        tools:ignore="UselessParent">

        <Button
            android:id="@+id/dummy_button"
            style="?metaButtonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/dummy_button" />

    </LinearLayout>
</FrameLayout>

我还在清单中指定了使用功能,如下所示:

    <uses-feature android:name="android.hardware.camera" android:required="false"/>

2 个答案:

答案 0 :(得分:0)

  

代码来自android文档,所以它真的很令人沮丧,它不起作用。

您似乎认为问题出在文档上。它不是。这是您选择的相机应用程序。

+----+-------------+-------+-------+---------------+------------+---------+------+---------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------------+---------+------+---------+-------+ | 1 | SIMPLE | t | index | indexedcolumn1 | indexedcolumn1 | 4 | NULL | 4514856 | NULL | +----+-------------+-------+-------+---------------+------------+---------+------+---------+-------+ 会启动一些相机应用。在大约10,000个Android设备型号上预装了几十个(如果不是数百个)不同的相机应用程序。用户可以从Play商店或其他地方安装数十个(如果不是数百个)其他相机应用程序。您的代码将开始执行其中任何一个。

其中一些相机应用程序存在错误。

你可以直接做到这一点。要么是因为某些相机应用程序存在错误,要么不使用ACTION_IMAGE_CAPTURE

答案 1 :(得分:-1)

使用此代码通过android Runtime权限实现此目的:

运行时允许我们需要API 23或大于23. Know more..

这是 MainActivity.Java 代码:我在这里使用了运行时权限,如果您要运行应用API 23或更高版本,这将显示dialog并询问权限。我使用过这里startActivityForResult将在捕获后返回图像。

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button ;
    ImageView imageView ;
    Intent intent ;
    public  static final int RequestPermissionCode  = 1 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);
        imageView = (ImageView)findViewById(R.id.imageView);

        EnableRuntimePermission();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent, 7);

            }
        });
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 7 && resultCode == RESULT_OK) {

            Bitmap bitmap = (Bitmap) data.getExtras().get("data");

            imageView.setImageBitmap(bitmap);
        }
    }

    public void EnableRuntimePermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.CAMERA))
        {

            Toast.makeText(MainActivity.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                    Manifest.permission.CAMERA}, RequestPermissionCode);

        }
    }

    @Override
    public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {

        switch (RC) {

            case RequestPermissionCode:

                if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(MainActivity.this,"Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();

                } else {

                    Toast.makeText(MainActivity.this,"Permission Canceled, Now your application cannot access CAMERA.", Toast.LENGTH_LONG).show();

                }
                break;
        }
    }

}

main_activity.xml 代码:这里我使用了ImageView和Button,点击按钮后它会捕获图像显示到imageview。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.MainActivity"
    android:orientation="vertical"
    android:background="#FFF9C4">


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/imageView" />

    <Button
        android:text="Click here to capture image using camera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button" />


</LinearLayout>

宣言代码:添加<uses-permission android:name="android.permission.CAMERA" />以获得相机许可。

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android_examples.captureimagecamera_android_examplescom">

    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>