无论图像有多大,onActivityResult byte []数据都返回null

时间:2014-12-30 18:40:44

标签: java android null bytearray

我正在尝试使用startActivityonActivityResult在活动之间传递字节数组。返回onActivityResult时,字节数组为空,我无法弄清楚原因。无论字节数组有多大,都会发生这种情况,所以我不认为它与大小有关。另外,我在另一个区域成功使用意图传递大小相同的字节数组。代码:

在帖子活动中:

public void callCropperIntent() {
         /* call the cropper to crop a photo from the gallery */
        intent = new Intent(getApplicationContext(), Cropper.class);
        startActivityForResult(intent, CROP_GALLERY_PICTURE);
    }

在裁剪活动中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    File croppedImageFile = new File(getFilesDir(), "test.jpg");
    try {
        if ((requestCode == REQUEST_PICTURE) && (resultCode == RESULT_OK)) {

            /** When the user is done picking a picture, we'll start the CropImage Activity,
             *  setting the output image file and size to 640 X 640 pixels square.
             */

            Uri croppedImage = Uri.fromFile(croppedImageFile);
            CropImageIntentBuilder cropImage = new CropImageIntentBuilder(640, 640, croppedImage);
            cropImage.setSourceImage(data.getData());
            cropImage.setOutputQuality(100);
            startActivityForResult(cropImage.getIntent(this), REQUEST_CROP_PICTURE);
        }
        else if ((requestCode == REQUEST_CROP_PICTURE) && (resultCode == RESULT_OK)) {
            /* when we are done cropping, send it back to PostActivity */
            //imageView.setImageBitmap(BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath()));
            Bitmap bmp = BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath());

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);

            //this set bitmap works so I know the bitmap is valid
            //test
            //imageView.setImageBitmap(bmp);

            byte[] imageData = stream.toByteArray();
            //bmp.recycle();

            Intent intent = getIntent();
            intent.putExtra("image", imageData);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

回到帖子活动:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    byte[] croppedData;

    /* check the request code */
    if (requestCode == CROP_GALLERY_PICTURE) {
        /* ensure the request was successful */
        if (resultCode == RESULT_OK) {
            /* The user picked and cropped a photo */

            /* retrieve photo from cropping activity */
            Intent intent = getIntent();
            croppedData = data.getByteArrayExtra("image");

            /* bytes ready to be sent to pg. 2 of posting process */
            callPostActivityPg2Intent(croppedData);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为在cropper类中,在finish()之前,你应该创建一个新的Intent类实例,而不是通过调用getintent()来获取它。

Ex:Intent intent = new Intent(); intent.putExtra()....