1

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();
                            }
                        }
                    }
                }
            }
        }
  • It will be very helpful to know what crash did you get and also on which of the devices you test this code – visionix visionix May 01 '16 at 14:09
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare May 01 '16 at 14:14

3 Answers3

2

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.

Savita
  • 747
  • 1
  • 7
  • 16
0

Hope this helps !

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,"Error !",Toast.LENGTH_LONG).show();
    else return true;                   
    }

                   }
Oggy.Coder
  • 43
  • 8
0
if (requestCode == 1) {

            if(data !=null){
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            //String path = saveImage(thumbnail);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            Glide.with(eventPreview.getContext()).load(byteArray).into(eventPreview);

            int width = eventPreview.getMeasuredWidth();
            eventPreview.getLayoutParams().height = width/2;
            eventPreview.setScaleType(ImageView.ScaleType.FIT_XY);

            postData = byteArray;

            PreviewTXT.setVisibility(View.GONE);}
        }
mate00
  • 2,727
  • 5
  • 26
  • 34
hnia
  • 1