拍照使应用程序崩溃[重复]

Posted

技术标签:

【中文标题】拍照使应用程序崩溃[重复]【英文标题】:taking picture make the app crash [duplicate] 【发布时间】:2018-10-07 04:02:12 【问题描述】:

我在 android studio 上开发了一个 android 应用程序。 我使用的是 LG G5,一切正常。我刚搬到三星s8,当我试图通过应用程序打开相机时,它崩溃了。 我尝试对此进行调试,发现该应用程序在以下位置崩溃: looper.java 在函数 loop() 中。 如何修复?

这是错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.imperialsoupgmail.tesseractexample, PID: 26176
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:6891)
                  at android.widget.TextView.performClick(TextView.java:12651)
                  at android.view.View$PerformClick.run(View.java:26083)
                  at android.os.Handler.handleCallback(Handler.java:789)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6938)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:6891) 
                  at android.widget.TextView.performClick(TextView.java:12651) 
                  at android.view.View$PerformClick.run(View.java:26083) 
                  at android.os.Handler.handleCallback(Handler.java:789) 
                  at android.os.Handler.dispatchMessage(Handler.java:98) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6938) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
               Caused by: java.lang.NullPointerException: file
                  at android.net.Uri.fromFile(Uri.java:453)
                  at com.imperialsoupgmail.tesseractexample.MainActivity.takePicture(MainActivity.java:258)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:6891) 
                  at android.widget.TextView.performClick(TextView.java:12651) 
                  at android.view.View$PerformClick.run(View.java:26083) 
                  at android.os.Handler.handleCallback(Handler.java:789) 
                  at android.os.Handler.dispatchMessage(Handler.java:98) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6938) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

【问题讨论】:

发布您的完整错误 发布您的错误日志 三星手机的摄像头确实会导致此问题,请在调用/使用摄像头之前尝试检查所有可用的摄像头模式。 【参考方案1】:

Hai Mizrachi,这个错误来自android os版本,所以请 按照下面的代码:

首先在以下代码中检查您的移动操作系统版本: `

private void selectImage() 
        final CharSequence[] items = "Take Photo", "Cancel";
        AlertDialog.Builder builder = new AlertDialog.Builder(MyProfileNew.this);
        builder.setTitle("Select Profile Image");
        builder.setItems(items, new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int item) 
                boolean result = checkPermission(MyProfileNew.this);
                if (items[item].equals("Take Photo")) 
                    userChoosenTask = "Take Photo";
                    if (result)


                        Log.v("current Version", String.valueOf(currentapiVersion));
                        Log.v("Bulid Version", String.valueOf(Build.VERSION_CODES.N));

                        if (currentapiVersion >= Build.VERSION_CODES.N) 
                            // Do something for lollipop and above versions
                            Log.v("current Version", String.valueOf(currentapiVersion));
                            Log.v("Bulid Version", String.valueOf(Build.VERSION_CODES.N));
                            Log.v("OS Version","N+");
                            startCamera();
                         else 
                            Log.v("OS Version","N-");
                            captureImage();
                        

                 else if (items[item].equals("Cancel")) 
                    dialog.dismiss();
                
            
        );
        builder.show();
    

如果操作系统版本小于N表示代码如下:

 private void startCamera() 
        try 
            dispatchTakePictureIntent();
         catch (IOException e) 
        
    

    private void dispatchTakePictureIntent() throws IOException 
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) 
            // Create the File where the photo should go
            File photoFile = null;
            try 
                photoFile = createImageFile();
             catch (IOException ex) 
                // Error occurred while creating the File
                return;
            
            // Continue only if the File was successfully created
            if (photoFile != null) 
                //Uri photoURI = Uri.fromFile(createImageFile());
                Uri photoURI = FileProvider.getUriForFile(MyProfileNew.this, BuildConfig.APPLICATION_ID + ".provider", createImageFile());
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            
        
    

如果操作系统版本大于N表示代码如下:

 //capture image from the camera
    private void captureImage() 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        // start the image capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
        System.out.println("Camera File URI"+fileUri);
    

上面的代码 100% 对我有用,试试这个代码。

【讨论】:

【参考方案2】:

您可以尝试以下示例,它在某种程度上对我有用。这是一个相当古老的代码。

List<String> supportedFocusModes = cameraParameters.getSupportedFocusModes();

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
           supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) 
    cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
 else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) 
    cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
 else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_MACRO)) 
    cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
 else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) 
    // auto focus on request only
    cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

mCamera.setParameters(cameraParameters);

【讨论】:

你把这段代码放在哪里了? @GuyMizrachi 显示应用程序崩溃的代码部分。 @GuyMizrachi 您可以在 open Camera 方法中使用它,或者基本上可以在任何地方使用它,您可以在其中检查相机空状态,或尝试打开相机,或访问参数。

以上是关于拍照使应用程序崩溃[重复]的主要内容,如果未能解决你的问题,请参考以下文章

当内存不足时,c ++分配器使应用程序崩溃[重复]

相机崩溃[重复]

将模型保存到 Userdefaults 会使应用程序迅速崩溃 [重复]

激活 iPhone 相机操作会使我的应用程序崩溃 [重复]

展开可选 JSON 元素会使应用程序崩溃 [重复]

在Redmi 7A上启动相机拍照时应用程序崩溃