【问题标题】:intent camera crashes after back press后按后意图相机崩溃
【发布时间】:2016-05-01 14:04:02
【问题描述】:

我正在尝试使用意图相机在我的应用程序中捕获视频,当我拍摄视频并被重定向到主要活动时,它一切正常,但是在我在意图相机模式下按下后退按钮后它崩溃了

p>
@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();
                            }
                        }
                    }
                }
            }
        }

【问题讨论】:

标签: java android android-intent parse-platform


【解决方案1】:

我猜你的相机视频意图代码是否 = RESULT_CODE_COMPRESS_VIDEO。然后将如下代码替换为

首先将resCode == RESULT_CODE_COMPRESS_VIDEO.更改为reqCOde 并使用以下代码

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();
                   }

               }

这不会使您的应用程序在从 Intent 摄像头被背压时崩溃。

【讨论】:

    【解决方案2】:

    希望这会有所帮助!

    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;                   
        }
    
                       }
    

    【讨论】:

      【解决方案3】:
      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);}
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-23
        • 1970-01-01
        • 2018-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多