Android - 相机意图导致应用程序崩溃

Posted

技术标签:

【中文标题】Android - 相机意图导致应用程序崩溃【英文标题】:Android - Camera Intent Causes the App to Crash 【发布时间】:2019-05-03 07:31:04 【问题描述】:

我正在尝试通过以下代码有意激活相机:

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity 
    private static final String TAG = "MainActivity";
    private ImageView mImageView;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    static final int REQUEST_TAKE_PHOTO = 1;

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

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

        button.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
                dispatchTakePictureIntent();
            
        );
    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        Log.d(TAG, "onActivityResult: 0");
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) 
            Log.d(TAG, "onActivityResult: 1");

            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            mImageView.setImageBitmap(imageBitmap);
        
    

    private void dispatchTakePictureIntent() 
        Log.d(TAG, "dispatchTakePictureIntent: 0");
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) 
            Log.d(TAG, "dispatchTakePictureIntent: 1");
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        
    

每次单击按钮时应用程序都会崩溃(它应该打开相机)。我可以从 logcat 获得“dispatchTakePictureIntent:1”,因此 takePictureIntent.resolveActivity(getPackageManager()) 不会返回 null。我认为是 startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE) 导致应用崩溃。

代码复制自官方文档:https://developer.android.com/training/camera/photobasics#java。

我尝试了 dispatchTakePictureIntent(View v),但应用仍然崩溃。我也用下面的方法替换了 dispatchTakePictureIntent() ,它仍然崩溃。

private void TakePicture()
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    startActivityForResult(intent, 0);

我一直在尝试在互联网上找到的其他解决方案,但没有一个有帮助。导致我的应用崩溃的问题可能是什么?

【问题讨论】:

检查是否在manifest文件中提到了摄像头权限。 使用 Logcat 检查与您的崩溃相关的 Java 堆栈跟踪,而不是猜测:***.com/q/23353173/115145 【参考方案1】:

我认为您没有使用任何权限检查,因为对于低于 23 的 android 设备不需要请求权限,但高于 23 您需要声明或要求用户访问 android 的功能。

如果设备运行的是 Android 6.0(API 级别 23)或更高版本,并且应用的 targetSdkVersion 为 23 或更高版本,则在安装时不会通知用户任何应用权限。您的应用程序必须要求用户在运行时授予危险权限。当您的应用程序请求权限时,用户会看到一个系统对话框(如图 1 左侧所示),告诉用户您的应用程序正在尝试访问哪个权限组。该对话框包括一个拒绝和允许按钮。

根据这个文档..

https://developer.android.com/guide/topics/permissions/overview


 if (ContextCompat.checkSelfPermission(thisActivity,    Manifest.permission.WRITE_CALENDAR)
        != PackageManager.PERMISSION_GRANTED) 
    // Permission is not granted



    // Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) 

    // Permission is not granted
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) 
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
     else 
        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]Manifest.permission.READ_CONTACTS,
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    
 else 
    // Permission has already been granted

来自 Android 文档的示例代码 https://developer.android.com/training/permissions/requesting#java

【讨论】:

我的 Manifest.xml 中有 ,还是我需要添加一些东西来实现权限检查? 正如@KarshSoni 已经提到的,您必须向用户询问相机许可,而不仅仅是在清单中声明它。在这里您可以了解更多信息:developer.android.com/training/permissions/requesting【参考方案2】:

请尝试在 android manifest 文件中赋予相机权限。 <uses-permission android:name="android.permission.CAMERA"> </uses-permission>。 然后尝试用这个Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivity(intent);替换你的意图代码

如果您不想使用设备相机,那么您也可以借助此库https://github.com/natario1/CameraView 制作自己的自定义相机

【讨论】:

【参考方案3】:

你可以这样使用

public static final int REQUEST_PICTURE_FROM_GALLERY = 10001;
public static final int REQUEST_PICTURE_FROM_CAMERA = 10002;

private File tempFileFromSource = null;
private Uri tempUriFromSource = null;
    
public void selectImageFromGallery() 
        
        try 

            tempFileFromSource  = File.createTempFile("choose", "png", context.getExternalCacheDir());
            tempUriFromSource   = Uri.fromFile(tempFileFromSource);

            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUriFromSource);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            
            context.startActivityForResult(intent, REQUEST_PICTURE_FROM_GALLERY);

         catch (IOException e) 
            e.printStackTrace();
        

    

 
    public void takePhotoWithCamera() 
        
        try 

            tempFileFromSource  = File.createTempFile("choose_", ".png", context.getExternalCacheDir());
            tempUriFromSource = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", tempFileFromSource);

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUriFromSource);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            
            context.startActivityForResult(intent, REQUEST_PICTURE_FROM_CAMERA);

         catch (IOException e) 
            e.printStackTrace();
        


    


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);
        
        if ((requestCode == REQUEST_PICTURE_FROM_GALLERY) && (resultCode == Activity.RESULT_OK)) 
            Log.d(TAG, "Image selected from gallery");
            prepareImage(data.getData(), tempFileFromSource);
         else if ((requestCode == REQUEST_PICTURE_FROM_CAMERA) && (resultCode == Activity.RESULT_OK)) 
            Log.d(TAG, "Image selected from camera");
            prepareImage(tempUriFromSource, tempFileFromSource);
        
    

    private void prepareImage(Uri uri, File imageFile) 
        try 

            if (uri == null)
                return;

            Bitmap bitmap = BitmapUtils.decodeBitmap(context, uri, 300, 300);

            if (bitmap == null)
                return;
            
            // now you have bitmap
         catch (IOException e) 
            e.printStackTrace();
        
    


    public static Bitmap decodeBitmap(Context context, Uri uri, int maxWidth, int maxHeight) throws IOException 

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        InputStream imageStream = context.getContentResolver().openInputStream(uri);
        BitmapFactory.decodeStream(imageStream, null, options);
        if (imageStream != null)
            imageStream.close();

        // Calculate inSampleSize
        options.inSampleSize = calculateSize(options, maxWidth, maxHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        imageStream = context.getContentResolver().openInputStream(uri);
        Bitmap img = BitmapFactory.decodeStream(imageStream, null, options);

        img = rotateImageIfRequired(context, img, uri);
        return img;
    

【讨论】:

以上是关于Android - 相机意图导致应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Android:使用相机意图时应用程序在 onActivityResult 上崩溃

由于 Activity 意图,Android 通知点击导致应用程序崩溃

相机意图应用程序崩溃

Kotlin 协程协程异常处理 ④ ( Android 协程中出现异常导致应用崩溃 | Android 协程中使用协程异常处理器捕获异常 | Android 全局异常处理器 )

Kotlin 协程协程异常处理 ④ ( Android 协程中出现异常导致应用崩溃 | Android 协程中使用协程异常处理器捕获异常 | Android 全局异常处理器 )

在flutter中从image_picker包中打开相机会导致真实设备上的应用程序崩溃,但在模拟器(android)中运行良好