Android: App crashes on onActivityResult while using Camera Intent

后端 未结 8 1568
一整个雨季
一整个雨季 2020-12-11 14:40

I am using camera intent to capture images in my App. The problem my app crashes on Android 5.0.2 while using camera. I am using intent from fragment. Below is

相关标签:
8条回答
  • 2020-12-11 15:21

    Here is what I often do in my projects, please take a look. Hope it helps!

    Fragment class:

    btnCapturePicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {       
            captureImage();     
        }
    });
    
    btnSelectPicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {       
            selectImage();
        }
    });
    
    ...
    
    private void captureImage() {
        mFileUri = Uri.fromFile(Utils.getOutputMediaFile(MEDIA_TYPE_IMAGE));
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    }
    
    private void selectImage() {    
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType(MIME_IMAGE_ALL); 
        startActivityForResult(intent, SELECT_PHOTO_CODE);
    }
    
    ...
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    
        switch (requestCode) {
            case SELECT_PHOTO_CODE:
                if (resultCode == Activity.RESULT_OK) {
                    mFileUri = imageReturnedIntent.getData();
                    if (mFileUri != null) {
                        mFilePath = Utils.getRealPathFromUri(mContext, mFileUri);
                        mFilePath = mFilePath.replace("file://", "");
                        // do something such as display ImageView...
                    }
                }
                break;
            case CAMERA_CAPTURE_IMAGE_REQUEST_CODE:
                if (resultCode == Activity.RESULT_OK) {
                    if (mFileUri != null) {
                        mFilePath = mFileUri.toString();
                        mFilePath = mFilePath.replace("file://", "");
                        // do something such as display ImageView...
                    }
                }
                break;
        }
    
        // refresh phone's folder content
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);     
            mediaScanIntent.setData(mFileUri);
            getActivity().sendBroadcast(mediaScanIntent);
        } else {
            getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        }
    }
    

    Utils class:

    //returning image, video
    public static File getOutputMediaFile(int type) {
    
        // External sdcard location
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");
    
        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }
    
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }
    
        return mediaFile;
    }
    
    //For select picture
    public static String getRealPathFromUri(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = {MediaStore.Images.Media.DATA};
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-11 15:28
        please check your path from mHighQualityImageUri because output path is not found, below code is modify please check it, it work and remember camera result is give in Main Activity class because You used Fragment so declare on activity Result in Main Activity (Fragment Activity) class.   
    //on Fragment Class
    private void takePhoto() {
                 /*   mHighQualityImageUri = Util.generateTimeStampPhotoFileUri(getActivity());
                    Log.d(UploadPicturesFragment.class.getSimpleName(),
                            "URI: " + mHighQualityImageUri.toString());*/
                    imageUri =Uri.fromFile(new File("/sdcard/"));
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    context.startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
                }
    
        //on Main Activity Class
          @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (requestCode == 1) {
                 /*   Log.d(UploadPicturesFragment.class.getSimpleName(),
                            "IMAGE URI NOT NULL: " + (mHighQualityImageUri == null));*/
                    try {
    
                        /*if(imageUri==null){
                            Log.i("Bitmap", "Image URI Null");
                        }else {
                            Log.i("Bitmap","Image URI is not Null");
                        }*/
                        Uri imageUri = Uri.fromFile(new File("/sdcard/"));
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUri);
        //                        mHighQualityImageUri);
                        if(bitmap !=null)
                        {
                            Log.i("Bitmap", "Bitmap not Null");
                        }else {
                            Log.i("Bitmap","Bitmap is Null");
                        }
                        //  DroomUtil.beginCrop(mHighQualityImageUri, getActivity(), this, true, bitmap.getWidth(),
                        // bitmap.getHeight());
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                super.onActivityResult(requestCode, resultCode, data);
            }
    
    0 讨论(0)
  • 2020-12-11 15:28

    On android, I have seen different devices exibit different behaviour for choosing image with camera and gallery. I find the better way is to:

    1. Create a content provider in your app.
    2. Get a Uri from your content provider and pass it to camera intent.
    3. The camera will write the captured image to your Uri.
    4. Read it using context.getContentResolver().openInputStream().

    This method make your code independent of the returned Uri in intent since you own the Uri. Also, this supports gallery image pick too with small modifications.

    I see you have device orientation issues too with camera. This (unfortunately) needs to be processed in your app one you acquire the image in a post processing step. I have outlined the code for it below as well. Mostly, orientation issues happened on Samsung devices where camera only captured images in landscape mode.

    Creating Uri for image:

    string imageId = "IMG" + System.currentTimeMillis();
    Uri attachmentUri = Uri.parse("content://"+ AttachmentContentProvider.AUTHORITY + "/images/" + imageId); 
    // Store this as a member in your activity/fragment as mAttachmentUri
    

    Note : Its important that you persist mAttachmentUri using shared preferences or activity bundle using onSaveInstanceState() otherwise the Uri may be lost when your app gets killed.

    Getting the camera intent:

    public static Intent getImageCaptureIntent(Context context, Uri outputFileUri)
    {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        return cameraIntent;
    }
    

    Reading the image:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
    
        if (requestCode == REQUEST_IMAGE_CAPTURE) 
        {
    
            try {
                Bitmap bitmap = decodeSampledBitmapFromResource(getActivity(), mAttachmentUri, Config.RGB_565);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
        public static Bitmap decodeSampledBitmapFromResource(Context context, Uri uri, Config config)
        { 
            Bitmap bmp = null;
            InputStream is = null;
            if (uri != null)
            {
                try
                {
                    is = context.getContentResolver().openInputStream(uri);
    
                    boolean resize = true;
                    // First decode with inJustDecodeBounds=true to check dimensions
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeStream(is, null, options);
                    mLogger.d("Image Original Width:" + options.outWidth + " Height:" + options.outHeight );
                    // close and open the stream again
                    is.close();
    
                    is = context.getContentResolver().openInputStream(uri);
                    int reqWidth = options.outWidth;
                    int reqHeight = options.outHeight;
    
                    // Decode bitmap with inSampleSize set
                    options.inJustDecodeBounds = false;
                    options.inPreferredConfig = config;     
                    bmp = BitmapFactory.decodeStream(is, null, options);
    
                    if(bmp != null)
                    {
                        bmp = correctImageRotation(context, bmp, uri);
                    }
                    else
                    {
                        mLogger.e("BitmapFactory.decodeStream returned null bitmap , skip correctImageRotation");
                    }
                }
                catch (FileNotFoundException fnfex)
                {
                    mLogger.e("FileNotFoundException : while decoding inline image bitmap: " + fnfex.getMessage());
                }
                catch (IOException ioex)
                {
                    mLogger.e("IOException : while decoding inline image bitmap: " + ioex.getMessage());
                }
                catch (OutOfMemoryError e)
                {
                    mLogger.e("OutOfMemoryError : in decodeSampledBitmapFromResource BitmapFactory.decodeStream . Skip loading Resource");
                }
                finally
                {
                    try
                    {
                        if (is != null)
                        {
                            is.close();
                        }
                    }
                    catch (IOException ioex2)
                    {
                        mLogger.e("IOException2 : while decoding inline image bitmap: " + ioex2.getMessage());
                    }
                }
            }
            return bmp;
        }
    
        // Seemed necessary on a lot of Samsung devices
        public static Bitmap correctImageRotation( Context context, Bitmap bitmap , Uri inputUri ) throws FileNotFoundException
        {
            int orientation = ExifInterface.ORIENTATION_UNDEFINED;
    
            try
            {
                String appfilesDir = context.getApplicationContext().getFilesDir().getAbsolutePath();
                String attachmentDirPath = appfilesDir + ('/') + "images");
                String fileName = ContentUris.parseId(uri) + ".jpg";
                String absolutePath = attachmentDirPath + ('/') + fileName;
                ExifInterface exif = new ExifInterface(path);
                orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
            }
            catch (IOException e)
            {
            }
    
            return rotateBitmap(bitmap, orientation);
        }
    
        /**
         * rotate bitmap code reference:
         * http://stackoverflow.com/questions/20478765/how-to-get-the-correct-orientation-of-the-image-selected-from-the-default-image
         */
        private static Bitmap rotateBitmap(Bitmap bitmap, int orientation)
        {
            Matrix matrix = new Matrix();
            switch (orientation)
            {
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            case ExifInterface.ORIENTATION_NORMAL:
            case ExifInterface.ORIENTATION_UNDEFINED:
            default:
                return bitmap;
            }
            try
            {
                Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                bitmap.recycle();
                return bmRotated;
            }
            catch (OutOfMemoryError e)
            {
                mLogger.e("OutOfMemoryError occured while rotating the image");
                return bitmap;
            }
        }
    

    Content provider:

    For the content provider implementation, you can use a android's FileProvider or implement a content provider like below. This content provider will open a file in your apps container for the camera app to write to.

    public class AttachmentContentProvider extends ContentProvider 
    {
        public static final String AUTHORITY = "com.yourcompany.yourapp.AttachmentContentProvider";
        public static final int ENTITY_ATTACHMENT = 1;
        public static final int ENTITY_ATTACHMENT_ID = 2;
    
        private static final UriMatcher sUriMatcher;
        static 
        {
            sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
            sUriMatcher.addURI(AUTHORITY, "images", ENTITY_ATTACHMENT);
            sUriMatcher.addURI(AUTHORITY, "images"+"/#", ENTITY_ATTACHMENT_ID);
        }
    
        @Override
        public boolean onCreate()
        {
            return true;
        }
    
        @Override
        public int delete(Uri uri, String where, String[] whereArgs)
        {
            return 0;
        }
    
        @Override
        public String getType(Uri uri)
        {
            int match = sUriMatcher.match(uri);
            switch (match) 
            {
            case ENTITY_ATTACHMENT:
            case ENTITY_ATTACHMENT_ID:
                return "image/jpeg";
    
            default:
                return null;
            }
        }
    
        @Override
        public Uri insert(Uri uri, ContentValues initialValues)
        {
            return null;
        }
    
        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
        {
            return null;
        }
    
        @Override
        public int update(Uri uri, ContentValues values, String where, String[] whereArgs)
        {
            return 0;
        }
    
        public static File getAttachmentFile(String fileName)
        {
            String appfilesDir = context.getApplicationContext().getFilesDir().getAbsolutePath();
            String attachmentDirPath = appfilesDir + ('/') + "images");
            File newFile = new File(AttachmentHelper.getAttachmentsDir() + File.separator + fileName);
            newFile.getParentFile().mkdirs();
            return newFile;
        }
    
    
        @Override
        public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException 
        {
            long id = -1;
            try
            {
                id = ContentUris.parseId(uri);
            }
            catch (NumberFormatException e)
            {
                m_logger.e("Invalid id for Uri : " + uri );
            }
    
            String filename  = id + ".jpg"; // id will be IMG+current time millis
            File imageFile = getAttachmentFile(id); 
            return (ParcelFileDescriptor.open(file, parseMode(mode)));
        }
    }
    

    To summarize, this code should work on most devices tested and does correction on the image received from camera if needed.

    0 讨论(0)
  • 2020-12-11 15:33

    Follow below steps in order to take picture from camera and display onto ImageView

    1) Start Camera Intent

    Uri fileUri;
    String photoPath = "";
    
     private void startingCameraIntent()
        {
            String fileName = System.currentTimeMillis()+".jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(intent, YOUR_REQ_CODE);
        }
    

    2) Callback onActivityResult Function

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            if (resultCode == Activity.RESULT_OK)
            {
                try
                {
                    photoPath = getPath(fileUri);
    
                    System.out.println("Image Path : " + photoPath);
    
                    Bitmap b = decodeUri(fileUri);
                    your_image_view.setImageBitmap(b);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    

    3) decodeUri Function

    private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException
        {
            BitmapFactory.Options o = new BitmapFactory.Options();
    
            o.inJustDecodeBounds = true;
    
            BitmapFactory.decodeStream(getContentResolver()
                                               .openInputStream(selectedImage), null, o);
    
            final int REQUIRED_SIZE = 72;
    
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
    
            int scale = 1;
    
            while (true)
            {
                if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                {
                    break;
                }
                width_tmp /= 2;
    
                height_tmp /= 2;
    
                scale *= 2;
            }
    
            BitmapFactory.Options o2 = new BitmapFactory.Options();
    
            o2.inSampleSize = scale;
    
            Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
                                                               .openInputStream(selectedImage), null, o2);
    
            return bitmap;
        }
    

    4) getPath of Image

    @SuppressWarnings("deprecation")
        private String getPath(Uri selectedImaeUri)
        {
            String[] projection = { MediaStore.Images.Media.DATA };
    
            Cursor cursor = managedQuery(selectedImaeUri, projection, null, null,
                                         null);
    
            if (cursor != null)
            {
                cursor.moveToFirst();
    
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    
                return cursor.getString(columnIndex);
            }
    
            return selectedImaeUri.getPath();
        }
    

    Finally In Manifest define permission

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    Note: If you are using marshmallow (Android 6.0) you have to set Permission checks before using camera app. You can read about Android Requesting Permissions at Run Time

    0 讨论(0)
  • 2020-12-11 15:36

    As per the logs

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
    

    It clearly states that you are calling getScheme() on a null object. So before processing, you need to check for null intent data.

    Also, beginning with Android M, you need to ask user for Camera permission. In lack of camera permission, app will crash.

    To know how to work with new permission architecture, look over it: http://developer.android.com/training/permissions/index.html

    0 讨论(0)
  • 2020-12-11 15:39

    First of all check if data is null in onActivityResult by adding an additional code which will prevent your app from being crashed, like this

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK && data !=null) {
        return;
    }
    }
    

    Then Consider This lesson Which explains how to capture photos using an existing camera application.

    There is a complete reference about Requesting camera permission using uses-feature tag , Getting a thumbnail, Saving full size photo, And many more which may be very helpful to you for accomplishing your Task ...

    0 讨论(0)
提交回复
热议问题