Search code examples
javaandroidandroid-intentparse-platform

intent camera crashes after back press


I am trying to use intent camera to capture video in my app,when i take a video and i am redirected to the Main activity it works all fine,however after i press back button in the intent camera mode it crashes

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        protected void onActivityResult(int reqCode, int resCode, Intent data) {
            if (reqCode == REQUEST_CODE_UPDATE_PIC) {
                if (resCode == RESULT_OK) {
                    String imagePath = data.getStringExtra(GOTOConstants.IntentExtras.IMAGE_PATH);
                    showCroppedImage(imagePath);
                    Bitmap bitmap;
                    bitmap = BitmapFactory.decodeFile(imagePath);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
                    byte[] image = stream.toByteArray();
                    // Create the ParseFile
                    ParseFile file = new ParseFile("profilepic.png", image);
                    // Upload the image into Parse Cloud
                    file.saveInBackground();
                    // Create a column named "ImageFile" and insert the image
                    user.put("profilepictures", file);
                    user.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            Toast.makeText(getApplicationContext(), "Saved Successfully",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
                    // Show a simple toast message
                    Toast.makeText(MainActivity.this, "Image Uploaded",
                            Toast.LENGTH_SHORT).show();
                } else if (resCode == RESULT_CANCELED) {
                    //TODO : Handle case
                } else {
                    String errorMsg = data.getStringExtra(ImageCropActivity.ERROR_MSG);
                    Toast.makeText(this, errorMsg, Toast.LENGTH_LONG).show();
                }
            }
            if (mediaFile != null) {
                compress();
            }
            if (resCode == Activity.RESULT_OK && data != null) {
                uri = data.getData();
                if (uri == null) {
                    Toast.makeText(getApplicationContext(), "uri blank", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "uri full", Toast.LENGTH_LONG).show();
                }
                //proceedbutton.setVisibility(View.VISIBLE);
                if (resCode == RESULT_CODE_COMPRESS_VIDEO) {
                    if (uri != null) {
                        Cursor cursor = getContentResolver().query(uri, null, null, null, null, null);
                        try {
                            if (cursor != null && cursor.moveToFirst()) {
                                String displayName = cursor.getString(
                                        cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                                Log.i(TAG, "Display Name: " + displayName);
                                int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
                                String size = null;
                                if (!cursor.isNull(sizeIndex)) {
                                    size = cursor.getString(sizeIndex);
                                } else {
                                    size = "Unknown";
                                }
                                Log.i(TAG, "Size: " + size);
                                tempFile = FileUtils.saveTempFile(displayName, this, uri);
                                editText.setText(mediaFile.getPath());
                            }
                        } finally {
                            if (cursor != null) {
                                cursor.close();
                            }
                        }
                    }
                }
            }
        }

Solution

  • I am guessing if your Camera Video Intent CODE = RESULT_CODE_COMPRESS_VIDEO. Then replace your code as below to

    First Thing change resCode == RESULT_CODE_COMPRESS_VIDEO.to reqCOde and use the following code

    if (reqCode == RESULT_CODE_COMPRESS_VIDEO) {
                       if(resCode == RESULT_OK) {
                       if (uri != null) {
                           compress();
                       }
                       }
                       else if(resCode == RESULT_CANCELED && data!=null){
                           Toast.makeText(MainActivity.this,"No Video Recorded",Toast.LENGTH_SHORT).show();
                       }
    
                   }
    

    This won't crash your application on backpressed from the intent camera.