我正在开发一个android应用程序。我在ViewPager的片段中使用自定义相机。
我有兼容性问题,我的应用程序可在android 4.2.2,4.4.4,5.0.1上运行。但是,如果我尝试使用Android版本5.1.0或5.1.1,则应用程序崩溃。我有这个日志:


  12/17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:FATAL EXCEPTION:main
  12/17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:进程:fr.jumaxdev.lekeen,PID:1999
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:java.lang.NullPointerException:尝试从空对象引用中的字段'int android.hardware.Camera $ Size.width'中读取
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at fr.jumaxdev.lekeen.Fragment_Photo.initPreview(Fragment_Photo.java:200)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at fr.jumaxdev.lekeen.Fragment_Photo.access $ 200(Fragment_Photo.java:35)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at fr.jumaxdev.lekeen.Fragment_Photo $ 5.surfaceChanged(Fragment_Photo.java:267)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:位于android.view.SurfaceView.updateWindow(SurfaceView.java:699)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at android.view.SurfaceView $ 3.onPreDraw(SurfaceView.java:200)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:位于android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1018)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:位于android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2301)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:位于android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1300)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:位于android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:7017)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:777)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at android.view.Choreographer.doCallbacks(Choreographer.java:590)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at android.view.Choreographer.doFrame(Choreographer.java:560)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:763)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:位于android.os.Handler.handleCallback(Handler.java:739)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:位于android.os.Handler.dispatchMessage(Handler.java:95)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at android.os.Looper.loop(Looper.java:145)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:位于android.app.ActivityThread.main(ActivityThread.java:6891)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at java.lang.reflect.Method.invoke(本机方法)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:372)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404)
  12-17 11:13:20.081 1999-1999 / fr.jumaxdev.lekeen E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)


因此,问题是“ java.lang.NullPointerException:尝试从null对象引用上的字段'int android.hardware.Camera $ Size.width'读取”,相机大小为null ..

这是一些代码:

@Override
public void onResume() {
    super.onResume();
    camera = getCamera("back");
    currentCamera = 1;
    startPreview();
}

@Override
public void onPause() {
    if (inPreview) {
        camera.stopPreview();
    }
    camera.release();
    camera = null;
    inPreview = false;

    super.onPause();
}

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int width, int height)
{
    // Source: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
    Camera.Size optimalSize = null;

    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio = (double) height / width;

    // Try to find a size match which suits the whole screen minus the menu on the left.
    for (Camera.Size size : sizes){

        if (size.height != width) continue;
        double ratio = (double) size.width / size.height;
        if (ratio <= targetRatio + ASPECT_TOLERANCE && ratio >= targetRatio - ASPECT_TOLERANCE){
            optimalSize = size;
        }
    }

    // If we cannot find the one that matches the aspect ratio, ignore the requirement.
    if (optimalSize == null) {
        // TODO : Backup in case we don't get a size.
    }

    return optimalSize;
}

private void initPreview(int width, int height) {
    if (camera != null && cameraPreviewHolder.getSurface() != null) {
        try {
            camera.setPreviewDisplay(cameraPreviewHolder);
        } catch (Throwable t) {
        }

        if (!cameraConfigured) {
            Camera.Parameters parameters = camera.getParameters();

            if (camera.getParameters().getSupportedPreviewSizes() != null){
                Camera.Size previewSize = getOptimalPreviewSize(camera.getParameters().getSupportedPreviewSizes(), width, height);;
                parameters.setPreviewSize(previewSize.width, previewSize.height);
            }
            camera.setParameters(parameters);
            cameraConfigured = true;
        }
    }
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void startPreview() {
    if (cameraConfigured && camera != null) {
        camera.startPreview();
        inPreview = true;
        camera.setDisplayOrientation(90);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            camera.enableShutterSound(false);
        }
    }
}


要恢复,如果Android版本> 5.0.1,则应用崩溃。但是,为什么要取决于android版本...?

如果有人能解决我的问题,我付他啤酒!
谢谢。
Attard Julien。

最佳答案

权限最近已更改。

在onCreate()中使用它:

String[] permissions = {"android.permission.CAMERA"};
int permissionRequestCode = 200;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED)
    {
        requestPermissions(permissions, permissionRequestCode);
    }


需要requestPermissionCode来检查用户是否在回调中实际接受了许可:“ onRequestPermissionsResult()”

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 200: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Permission Granted
            } else {
                //Permission Denied
            }
            return;
        //Use other case lines for other requests (with different requestCodes)
        }
    }
}

关于java - Android Camera-尝试从空对象引用中读取字段'int android.hardware.Camera $ Size.width',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34332520/

10-08 22:54