【问题标题】:Android: Camera.Open() returns null with one Project but Not the other. Same DeviceAndroid:Camera.Open() 返回 null 与一个项目,但不是另一个。同一设备
【发布时间】:2016-05-12 06:09:31
【问题描述】:

我正在学习 Android Camera Api,我正在 Nexus 6 设备上对其进行测试。我下载了一个在设备上运行良好的 Camera Api 教程。然后我创建了自己的项目并基本上复制了代码,但每当我调用 Camera.open() 时,该代码总是返回 null。代码几乎相同。

清单也是相同的,我的 gradle 文件也是如此。

但是我从网站下载的教程有效,而我从 Android Studio 创建的教程无效。

我无法弄清楚我做错了什么。我已经检查了有关此问题的先前问题,但我没有犯同样的错误。清单权限位于正确的位置(在应用程序标签上方),Nexus 6 有一个后置摄像头,因此没有摄像头不是问题。

非工作清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gr3ymatter.cameraapidemo">
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

非工作 MainActivity:

import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    private ImageSurfaceView mImageSurfaceView;
    private Camera camera;

    private FrameLayout cameraPreviewLayout;
    private ImageView capturedImageHolder;

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

        cameraPreviewLayout = (FrameLayout)findViewById(R.id.camera_preview);
        capturedImageHolder = (ImageView)findViewById(R.id.captured_image);

        camera = checkDeviceCamera();
        mImageSurfaceView = new ImageSurfaceView(MainActivity.this, camera);
        cameraPreviewLayout.addView(mImageSurfaceView);

        Button captureButton = (Button)findViewById(R.id.button);
        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                camera.takePicture(null, null, pictureCallback);
            }
        });
    }
    private Camera checkDeviceCamera(){
        Camera mCamera = null;
        try {
            mCamera = Camera.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mCamera;
    }

    Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            if(bitmap==null){
                Toast.makeText(MainActivity.this, "Captured image is empty", Toast.LENGTH_LONG).show();
                return;
            }
            capturedImageHolder.setImageBitmap(scaleDownBitmapImage(bitmap, 300, 200 ));
        }
    };

    private Bitmap scaleDownBitmapImage(Bitmap bitmap, int newWidth, int newHeight){
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
        return resizedBitmap;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

非工作 SurfaceView 类

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

/**
 * Created by Afzal on 2/2/16.
 */

public class ImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private Camera camera;
    private SurfaceHolder surfaceHolder;

    public ImageSurfaceView(Context context, Camera camera) {
        super(context);
        this.camera = camera;
        this.surfaceHolder = getHolder();
        this.surfaceHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            this.camera.setPreviewDisplay(holder);
            this.camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        this.camera.stopPreview();
        this.camera.release();
    }
}

Working Projects 文件与我在遵循示例时从字面上复制的文件相同。如果有帮助,我可以将 github 链接粘贴到工作项目和非工作项目?

编辑:这是两个项目的回购 https://github.com/Gr3ymatter/CameraApiDemo_Working https://github.com/Gr3ymatter/CameraApiDemo_NotWorking

编辑:LOGCAT 输出。同样基于 Madhurs 有用的 cmets,我认为这可能是一个设备问题,所以我在 Nexus 7 上运行了它,但我仍然遇到了同样的问题。

提前感谢您的帮助!

【问题讨论】:

    标签: android android-camera android-permissions


    【解决方案1】:

    由于 Android 6.0,您需要在 nexus 6 中询问权限 查看permissions的更多信息

    或者您可以在清单中更改为,

    //make target lower then 23 then it will run properly in android M
        <uses-sdk
                android:minSdkVersion="15"
                android:targetSdkVersion="22" />
    

    无权限示例

    查看此示例,希望对您有所帮助,

    https://github.com/pikanji/CameraPreviewSample

    你的代码对我有用

    【讨论】:

    • 感谢您的评论,不幸的是这样做并没有改变任何事情。令人困惑的是,工作项目与我的权限相同。
    • dude 查看 manifest 中的 targetsdk 和 gradle 中已编译的 sdk 它应该在 22 岁以下,并检查工作的项目
    • 我查过了。我还在这里发布了这两个项目的存储库,以便您自己查看。 manifest 和 gradle 文件相同,都在 22 以上。两者都设置为 23。github.com/Gr3ymatter/CameraApiDemo_NotWorkinggithub.com/Gr3ymatter/CameraApiDemo_Working
    • 我有一些时间,所以我检查了你的代码,它运行良好,我将在我的答案中分享它的屏幕截图
    • Madhur,非常感谢您抽出宝贵时间。这很有趣。我想知道为什么它在我的 nexus 6 上不起作用。我用我在 NOTWORKING 代码中得到的 logcat 输出的屏幕截图更新了答案。我不断为相机对象获取空值。注意包名是一样的。我的工作代码没有任何此类问题。起初我想也许相机没有正确释放并且可能很忙,但它在工作代码中正常工作。很奇怪。
    猜你喜欢
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2022-09-27
    相关资源
    最近更新 更多