2016-02-03 23 views
0

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

清单也是一样的,我的gradle文件也是一样的。

但是我从网站下载的教程正常工作,而我从Android Studio创建的教程却没有。

我不知道我做错了什么。我检查了以前有关这个问题的问题,我不犯同样的错误。 Manifest权限位于适当位置(位于应用程序标签上方),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(); 
    } 
} 

工作项目文件与我在按照示例进行复制时一样。如果有帮助,我可以将github链接粘贴到工作和非工作项目中?

编辑:这里有两个项目 https://github.com/Gr3ymatter/CameraApiDemo_Working https://github.com/Gr3ymatter/CameraApiDemo_NotWorking

编辑回购:logcat的输出。也基于Madhurs有用的评论,我想也许这是一个设备问题,所以我在Nexus 7上运行这个,但我仍然有同样的问题。

enter image description here

预先感谢您的帮助!

回答

2

你要问权限的Nexus 6,因为更多信息的Android 6.0 支票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

你的代码是为我工作

image

+0

感谢您的评论,不幸的是这样做没有改变任何东西。令人困惑的是,工作项目与我的权限相同。 – user1953478

+0

家伙看到在gradle这个清单和编译过的SDK中的targetsdk它应该是在22,并检查其工作 – Madhur

+0

我检查项目。我也在这里发布了这两个项目的回购,以便您可以亲眼看到。清单和gradle文件是相同的,并在22以上。两者都设置为23. https://github.com/Gr3ymatter/CameraApiDemo_NotWorking https://github.com/Gr3ymatter/CameraApiDemo_Working – user1953478

相关问题